ctl has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to find my local UTF offset. For some reason, this is one hour too small (in absolute value):

$t = POSIX::mktime(localtime) - POSIX::mktime(gmtime);

I'm UTC -04:00, but that tells me that I'm only UTC -03:00. Can anyone explain why? (if it matters, I'm on Debian with "v5.8.4 built for i386-linux-thread-multi")

Update: I was being an idiot and was actually getting -05:00 instead of -04:00. Anyhow, the solution is to do

@g=gmtime;$g[8]=-1;$offset=POSIX::mktime(localtime)-POSIX::mktime(@g);

Since gmtime won't set DST, and when you're in it, that will mess you up (since mktime() takes local time, which in a DST timezone, expects to be in DST).


With humble thanks,
    -Chris
--
"They laughed at Einstein. They laughed at the Wright Brothers. But they also laughed at Bozo the Clown." -- Carl Sagan

Replies are listed 'Best First'.
Re: UTC offset
by ikegami (Patriarch) on Jun 09, 2005 at 01:09 UTC
    >perl -le "use POSIX; print((POSIX::mktime(localtime) - POSIX::mktime( +gmtime)) / 60 / 60);" -5

    ...which is perfectly appropriate, since I'm in EST. If it was winter, I'd expect to see -4, because then I'd be in EDT. Maybe your OS's time zone isn't set right? Does the (unix) system tool date return the right time zone? The (linux and BSD) time zone is changed by altering to where the link /etc/localtime points. Where are you located, anyway?

Re: UTC offset
by mugwumpjism (Hermit) on Jun 09, 2005 at 01:23 UTC

    To explore this problem, print;

    • the values returned by list context localtime
    • the values returned by list context gmtime

    Then, try passing time() + 6 * 30 * 86400 as the explicit argument to both functions, and look at the same thing. You will see where the apparent anomaly is coming from.

    $h=$ENV{HOME};my@q=split/\n\n/,`cat $h/.quotes`;$s="$h/." ."signature";$t=`cat $s`;print$t,"\n",$q[rand($#q)],"\n";