On Wed Jan 05 17:15:16 2005, guest wrote:
Show quoted text> See attached for example code.
Sorry, but I don't think that's correct at all. I realize this thread
is old, but I ran into this page while looking for a similar method.
You can't just subtract the "gm hour" from the "local hour" and reliably
get the timezone offset. That doesn't take into account the case where
"gm hour" and "local hour" are actually hours of different days.
In my case I need the timezone on windows in order to produce an rfc822
date string (ala unix' "date -R"), and so far the easiest way I've come
up with to get the timezone offset is below. The idea is to feed the
output of both localtime and gmtime back into timelocal to get an epoch
time for each of them as though they were different times in the same
timezone, then compute the offset as a function of the difference in
epoch seconds.
#!/usr/bin/perl
use Time::Local;
my $local=scalar localtime;
my $gm=scalar gmtime;
my @locals=(localtime)[0,1,2,3,4,5];
my @gms=(gmtime)[0,1,2,3,4,5];
my $localepochtime=timelocal(@locals);
my $gmepochtime=timelocal(@gms);
my $seconds_apart=$localepochtime-$gmepochtime;
# Admittedly the rounding isn't exactly correct according to
# the conventions of science.
if ($seconds_apart<0) {
print "Offset=", int( ($seconds_apart/60/60) - .5), "\n";
} else {
print "Offset=", int( ($seconds_apart/60/60) + .5), "\n";
}