$ENV{'TZ'} = 'CST6CDT';
I actually have a library that I include in all my scripts, so I just set this variable in that library. Works great!
If you're not familiar with the syntax, it's the abbreviation for standard time, followed by the hours to add to get GMT, then the abbreviation for daylight savings time. For example, if you're in California, it would be: PST8PDT
(This should work fine on most Unix-like servers. Not sure about Windows.)
#!/usr/bin/perl
#$ENV{'TZ'} = 'GMT0GMT';
#$ENV{'TZ'} = 'EDT5EST';
#$ENV{'TZ'} = 'CDT6CST';
#$ENV{'TZ'} = 'MDT7MST';
$ENV{'TZ'} = 'PDT8PST';
# get the time stamp on the file
$file = '/tmp/test.html';
$stamp = (stat($file))[9];
($sec,$min,$hr,$mday,$mon,$yyyy,$wday,$yday,$isdst) = localtime($stamp);
# convert military to English
$ampm = ($hr < 12)? 'am' : 'pm';
if ($hr < 1) { $hr = 12; }
elsif ($hr > 12) { $hr -= 12; }
# make the date parts pretty
$mm = sprintf("%02d", $mon + 1);
$dd = sprintf("%02d", $mday);
$yyyy += 1900;
# make the time parts pretty
$hr = sprintf("%02d", $hr);
$min = sprintf("%02d", $min);
$sec = sprintf("%02d", $sec);
print "The file was last updated on ${mm}/${dd}/${yyyy} at ${hr}:${min}:${sec} ${ampm}\n";
exit 0;
A great implementation of this is if you have visitors who login to your site, and you know their local time zone, you can make it dynamic based on the user's preferences. (Or you can get fancy and use one of those databases that gives you a city and state based on an I.P. address, though those aren't 100% reliable.)
Note: This script works great on my Linux server. However, I just tested on Windows, and the TZ environment variable has no effect; it always displays the time in my local zone. But, knowing that it works on Linux, perhaps there is some similar solution for Windows. Hmmm.
#!perl
print "Content-type: text/html\n\n";
foreach $key (sort keys(%ENV)) {
printf("%-10.20s: $ENV{$key}\n", $key);
}
exit;
but it does change the time output to the corresponding time zone when I run the script you posted. :)