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

While porting code using DateTime to Windows I got a "Cannot determine local time zone" error. The code is like:
DateTime->from_epoch(epoch => $t, time_zone => 'local');
Is there a way to get a time zone from Windows? From strftime I got something semi-useful:
C:\>perl -MPOSIX=strftime -le "print strftime(q(%Z), localtime)" AUS Central Standard Time
I decided to calculate the difference between localtime and gmtime for now. Is there a better alternative?
#!/usr/bin/perl -w use strict; use DateTime::Duration; my ($lh, $lm) = (localtime)[2,1]; my ($gh, $gm) = (gmtime)[2,1]; my $dur = DateTime::Duration->new( hours => ($lh-$gh), minutes => ($lm +-$gm)); print sprintf "%+02.2d%02d\n", $dur->in_units('hours','minutes');
Thanks, Brad

Replies are listed 'Best First'.
Re: Determining Time Zone on Win32
by ashokpj (Hermit) on May 10, 2006 at 06:47 UTC

    Try this module Time::Piece

    This module replaces the standard localtime and gmtime functions with implementations that return objects. It does so in a backwards compatible manner, so that using localtime/gmtime in the way documented in perlfunc will still return what you expect

    you can also able find time different

    $t1 - $t2; # returns Time::Seconds object $t1 - 42; # returns Time::Piece object $t1 + 533; # returns Time::Piece object
      Time::Piece::tzoffset looks good, thanks
      perl -MTime::Piece -le '$m=localtime->tzoffset->minutes; printf "%+02. +2d%02.2d\n", int($m/60), $m%60' +1000 # cmd.exe friendly perl -MTime::Piece -le "$m=localtime->tzoffset->minutes; printf qq(%+0 +2.2d%02.2d\n), int($m/60), $m%60" +1000