Keith's Software and Tutorials Home Page
Knowledge Is Power

Use A Different Timezone
Time In Your Perl Script



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. However, you want to run CGI scripts that use time functions and you want them to use your local time rather than the server-based UTC time.

Luckily it's an easy fix. In your Perl script you just read the server's UTC time into a variable and modify it with the offset values for Standard and Daylight Saving times in your timezone. The server will even tell you if it's using Standard or Daylight Saving time so you can adjust the offset value accordingly.

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 timezones and positive for European timezones. If you're unsure of what offset values to use, a simple Google search for 'UTC offset values for...' your location will provide the correct values.

NOTE:  If your area does not observe a summer time adjustment such as Daylight Saving Time (DST) or Central European Summer Time (CEST) then enter the same value for both statements below just to be safe.


# Perl script to offset server UTC time
# to local time.

# For Paris/Rome CET use +1 and +2 below
# For the UK use +0 and +1 below
# For US Eastern Time use -4 and -5 below
# For US Central Time use -5 and -6 below
# For US Mountain Time use -6 and -7 below
# For US Pacific Time use -7 and -8 below

# This script is set for US Eastern Time

   #--- Read current server date/time values into variables
($sec,$min,$hour,$today,$month,$year,$dayofweek,$dayofyear,$isDST) = localtime(time);

if ($isDST)
{
 $hour = $hour -4;
}
else
{
 $hour = $hour -5;
}

if ($hour < 0)
{
 $hour = $hour + 24;
 $dayofweek = $dayofweek - 1
 $dayofyear = $dayofyear - 1
}


Then you can use these adjusted variables ($hour $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.

Want To Learn The Right Way To Trade Options? - See my TastyTrade Review



Top of page





Contents, diagrams, and images    Copyright © 2004-2023    Keith Parkansky    All rights reserved.
Certain graphics, symbols, and terms used on this site and in its documents are registered trademarks
of their respective owners and are contained herein for identification purposes only.
No endorsement of this site, its contents, or its documents by these owners is expressed or implied.

LIABILITY
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.