![]() Knowledge Is Power |
Use A Different Time Zone
|
Scenario
You have your Web site hosted on a server with a provider who sets the server times to UTC (aka GMT or Zulu) time and you want to run CGI scripts that use time functions. The problem is UTC time doesn't change for Daylight Saving Time so you need to use your local time zone rather than the server-based UTC time zone.
Luckily there's a very easy fix if Perl is appropriately configured on the Web server. The time zone the server is using is stored in an enviroment variable calledTZ so you need to change that environment variable before you call thelocaltime() function. Simply add the following statements near the top of your script:
# use POSIX qw(tzset); $ENV{TZ} = 'America/New_York'; # TZ names @ https://en.wikipedia.org/wiki/List_of_tz_database_time_zones # tzset; # Should not needed as it'll be run automatically with localtime() below # The localtime() function must come AFTER the above statements ($sec,$min,$hour,$day,$month,$year,$dayofweek,$dayofyear,$isDST) = localtime(time);
Note that thetzset anduse commands are commented out. This is because on POSIX-compliant systems thelocaltime function that follows should runtzset automatically. If using just the$ENV statement doesn't reset the system timezone to your time zone while the script is executing, try uncommenting thetzset anduse commands and see if that rectifies the problem.
NOTE: If the server timezone is set to UTC, the$isDST variable above will always be 0 (zero).
If uncommenting thetzset anduse commands don't rectify the problem it's likely your Web server isn't configured appropriately. You can try contacting technical support at your Web hosting provider. If that doesn't work you'll have to resort to the code below.
In the code below we simply read the server's UTC time into a variable and modify it with the offset values for Standard and Daylight Saving times in your time zone. If you're unsure of what offset values to use, check the"UTC Offset" columns on this Wikipedia page:
https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
Just add the code given below near the beginning of your script. Note the offset values given in the script comments below are negative for US time zones and positive for European time zones.
NOTE: Using this method means you'll have to change the offset value MANUALLY every time there is a time change.
# Perl script to offset server UTC time # to local time. # For STANDARD Time use: # Paris/Rome CET use +2 below # UK use +1 below # US Eastern Time use -5 below # US Central Time use -6 below # US Mountain Time use -7 below # US Pacific Time use -8 below # For DAYLIGHT SAVING Time use: # Paris/Rome CET use +1 below # UK use 0 below # US Eastern Time use -4 below # US Central Time use -5 below # US Mountain Time use -6 below # US Pacific Time use -7 below #--- Read current server date/time values into variables ($sec,$min,$hour,$day,$month,$year,$dayofweek,$dayofyear,$isDST) = localtime(time); $hour = $hour -5; # EDIT THIS OFFSET VALUE (now set to US Eastern STANDARD Time) if ($hour < 0) { $hour = $hour + 24; $day = day - 1; $dayofweek = $dayofweek - 1; $dayofyear = $dayofyear - 1; }
Then you can use these adjusted variables ($hour $day $dayofweek $dayofyear) throughout your script. Note that the reason you want to define and initialize these variables near the top of your script is so they're defined globally for use throughout your script.
IN NO EVENT WILL KEITH PARKANSKY BE LIABLE TO ANY PARTY (i) FOR ANY DIRECT, INDIRECT, SPECIAL, PUNITIVE OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION, LOSS OF PROGRAMS OR INFORMATION, AND THE LIKE), OR ANY OTHER DAMAGES ARISING IN ANY WAY OUT OF THE AVAILABILITY, USE, RELIANCE ON, OR INABILITY TO USE THE INFORMATION, METHODS, HTML OR COMPUTER CODE, OR "KNOWLEDGE" PROVIDED ON OR THROUGH THIS WEBSITE OR ANY OF ITS' ASSOCIATED DOCUMENTS, DIAGRAMS, IMAGES, REPRODUCTIONS, COMPUTER EXECUTED CODE, OR ELECTRONICALLY STORED OR TRANSMITTED FILES OR GENERATED COMMUNICATIONS OR DATA EVEN IF KEITH PARKANSKY SHALL HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES, AND REGARDLESS OF THE FORM OF ACTION, WHETHER IN CONTRACT, TORT, OR OTHERWISE; OR (ii) FOR ANY CLAIM ATTRIBUTABLE TO ERRORS, OMISSIONS, OR OTHER INACCURACIES IN, OR DESTRUCTIVE PROPERTIES OF ANY INFORMATION, METHODS, HTML OR COMPUTER CODE, OR "KNOWLEDGE" PROVIDED ON OR THROUGH THIS WEBSITE OR ANY OF ITS' ASSOCIATED DOCUMENTS, DIAGRAMS, IMAGES, REPRODUCTIONS, COMPUTER EXECUTED CODE, OR ELECTRONICALLY STORED, TRANSMITTED, OR GENERATED FILES, COMMUNICATIONS, OR DATA. USE OF THIS SITE CONSTITUTES ACCEPTANCE OF ALL STATED TERMS AND CONDITIONS.