Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

TimeZoning oddities

by JPaul (Hermit)
on Nov 01, 2005 at 22:49 UTC ( #504771=perlquestion: print w/replies, xml ) Need Help??

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

Greetings monks;

I'm trying to get a very simple timezone display going without having to install the rather heavy DateManip or DateTime groups of modules (Which seemingly require copious amounts of modules).
I live in CST, and the following works:

$ENV{TZ} = "America/New_York"; my $now_string = strftime "%a %b %e %H:%M:%S %Y %c", localtime; print "$now_string\n";
Which prints:
Tue Nov  1 17:40:54 2005 Tue 01 Nov 2005 05:40:54 PM EST
However, if I try the following:
$ENV{TZ} = "America/Chicago"; my $now_string = strftime "%a %b %e %H:%M:%S %Y %c", localtime; print "$now_string\n"; $ENV{TZ} = "America/New_York"; $now_string = strftime "%a %b %e %H:%M:%S %Y %c", localtime; print "$now_string\n";
I get:
Tue Nov  1 16:41:08 2005 Tue 01 Nov 2005 04:41:08 PM CST
Tue Nov  1 16:41:08 2005 Tue 01 Nov 2005 04:41:08 PM EST

Which is clearly more than a little odd.
I'm not seeing an obvious difference here in methodology - which means that there's either a bug in localtime(), or I'm missing something - and I'm putting money on it being my problem.

I can, of course, move to using one of the larger modules - that is a valid option, but I'd prefer to not have to resort to doing so when it appears localtime() should be able to handle this properly.
(I'm on a Linux machine running Debian, if it helps)

My thanks;

-- Alexander Widdlemouse undid his bellybutton and his bum dropped off --

Replies are listed 'Best First'.
Re: TimeZoning oddities
by robin (Chaplain) on Nov 01, 2005 at 23:26 UTC

    Update: See my second reply below.

    Old Update: This is a bug in old versions of the POSIX module: see here, for example. I see this bug with Perl 5.8.0, but not with 5.8.6. (I don't know exactly which version fixed it.) Is upgrading an option for you? If not, you might have to use one of those other modules after all...

    PPS. Looks like downgrading would also fix it: it's working properly in 5.6.2.

    PPPS. But don't upgrade too much! I get the bug again with 5.8.7. This is very odd. Maybe it's affected by the build options or something, but I can't see an obvious pattern.

    I can only offer the faintly unhelpful "it works for me": with your code I get:

    Tue Nov 1 17:16:55 2005 Tue 01 Nov 2005 17:16:55 CST Tue Nov 1 18:16:55 2005 Tue 01 Nov 2005 18:16:55 EST
    which seems reasonable. I can only suppose the problem is with your system rather than with Perl. What do you get if you do
    env TZ=America/New_York date env TZ=America/Chicago date
    in the shell?

    Also, do you get the correct offset if you add %z to your strftime format?

Re: TimeZoning oddities
by robin (Chaplain) on Nov 02, 2005 at 12:13 UTC

    You can fix the problem by calling POSIX::tzset() after changing $ENV{TZ}.

    It could be that this is the way it's supposed to work, and it's not a bug at all; although the behaviour is certainly inadequately documented and inconsistent. (BTW: the bug I was referring to is here, but I think it's not quite the same problem.)


      I saw that in one of the PODs I was reading through - but since all the examples I had found (including on PM) did not include it, I assumed that it was not really necessary. Doofus.

      Thank you very much, this does the magic.

      -- Alexander Widdlemouse undid his bellybutton and his bum dropped off --

        At last I have found the definitive answer! The problem is with localtime rather than strftime.

        This problem happens only on Linux, and only with a threaded build of perl. There's some disgreement about whether the bug is in Linux or perl, but in either case a manual call to tzset is a good workaround.

        See the bug report and ensuing discussion here.

Re: TimeZoning oddities
by swampyankee (Parson) on Nov 02, 2005 at 04:00 UTC

    I'm doing most of my work on XP right now, so I can't comment on Debian. I've had my own timezone mysteries (for some reason half the apps I've written -- those not in Perl-- are convinced I'm using UTC).

    I'm wondering if the time you're getting (from the implied call to time) is being assumed to be in the local time, with the time zone merely being tacked on. What happens to the return from gmtime() with a change in time zone?


Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://504771]
Approved by kirbyk
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2023-05-31 03:51 GMT
Find Nodes?
    Voting Booth?

    No recent polls found