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

Before I attack the code with the debugger, I'd like to see if anybody has already figured this out...

I want to convert the times used to a consistent timezone (we'll say US/Central)

For starters, I use the following code to get the system time on my box

#!/usr/bin/perl
use Date::Manip
print UnixDate( ParseDate('today'), "%A, %B %E, %Y -- %R %Z\n");
It yields the following output:
Monday, March 19th, 2001 -- 18:44 MST
Now, according to the perldoc for Date::Manip, this code should do the conversion:
#!/usr/bin/perl
use Date::Manip;
Date_Init("ConvTZ=CST");
print UnixDate( ParseDate('today'), "%A, %B %E, %Y -- %R %Z\n");
But this is what it gives me:
Monday, March 19th, 2001 -- 20:52 CST

Aside from the fact that I get myriad errors when trying to pass 'US/Central' into ConvTZ with Date_Init. And if I just set $Date::Manip::TZ with Date_Init, no conversion takes place (it just changes what you get when you use a %Z token).

Apparently it's adding two hours instead of just one. Even better, if I tell it to convert from the local time (which is MST) to CDT, it adds four hours.

Am I doing something wrong, or is this a bug in Date::Manip? Any suggestsions?

Thanks, folks.

Replies are listed 'Best First'.
Re: Converting timezones with Date::Manip
by voyager (Friar) on Mar 20, 2001 at 10:15 UTC
    try this:
      my $already = exists $ENV{TZ};
      my $original_time_zone = $ENV{TZ} if $already;
      $ENV{TZ} = 'some other time zone';
      $string_time = POSIX::strftime("%Y-%m-%d %H:%M:%S", localtime($epoch_time));
      $ENV{TZ} = $original_time_zone if $already;
      delete $ENV{TZ} unless $already;
    
    it's seems crude, but temporarily changing the time zone for the system was what it took. (for me it was just an apache child)
Re: Converting timezones with Date::Manip
by Masem (Monsignor) on Mar 20, 2001 at 07:11 UTC
    It looks like a bug there, but I do notice that there is the Date_ConvTZ function, which you can convert to a timezone, or between two timezones, which does appear to work right.
    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
Re: Converting timezones with Date::Manip
by SamQi (Beadle) on Mar 20, 2001 at 07:01 UTC

    (That was me...didn't realize I wasn't logged in...ooopsie)

    The other curiosity that crops up is that when I assign a timezone in the format "US/Central" or "CST6CDT" to ConvTZ it spits out lots of errors then prints the string without any timzeone translation (on my box at work), here it just doesn't do the timezone translation (here == home).

Re: Converting timezones with Date::Manip
by SamQi (Beadle) on Mar 20, 2001 at 07:11 UTC

    One more peculiarity that I just turned up...

    If I do print Date_ConvTZ( ParseDate('now'), 'MST', 'CST' ) the conversion works correctly. Bizarre