Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

How to set a processes notion of the timezone on win32?

by jaldhar (Vicar)
on Mar 02, 2010 at 17:19 UTC ( #826206=perlquestion: print w/ replies, xml ) Need Help??
jaldhar has asked for the wisdom of the Perl Monks concerning the following question:

Does anyone have a clue how to do this? On Linux I can do this:

BEGIN { $ENV{TZ} = 'UTC'; } say scalar localtime;

...and localtime will give me the time in UTC. However win32 (Windows Vista with strawberry Perl 5.10.1 to be precise) steadfastly refuses to give me anything except the local time. Though funnily enough, if I do:

set TZ="UTC"
...at the windows command prompt, then print scalar localtime will be in UTC. So there has to be some way to do this in perl but what is it?

(Some background. My module Module::Starter::Plugin::CGIApp creates a bunch of files. One of these has a time stamp created with localtime. In my test suite I compare the modules output against a set of known good files. However as the value of localtime keeps changing, I need to force a fixed time in the test script. I use Test::MockTime for this which almost works except for this time zone issue.)

--
જલધર

Comment on How to set a processes notion of the timezone on win32?
Select or Download Code
Re: How to set a processes notion of the timezone on win32?
by almut (Canon) on Mar 02, 2010 at 17:39 UTC

    I dimly remember once having had a similar issue, which - IIRC - was solved by re-exec-ing the program after having changed the environment variable (not entirely sure though — can't find the code and don't have a Windows system to try at the moment...)

Re: How to set a processes notion of the timezone on win32?
by banesong (Acolyte) on Mar 02, 2010 at 17:46 UTC
    I had a similar problem with an application that had to return local times for multiple timezones. An easy enough way to do this is to use the DateTime module. To convert a known time to another time, you can use:
    my $oldDate = DateTime->new(month=>$monthVar, day=>$dayVar, year=>$yearVar, hour=>$hourVar, minute=>$minuteVar, time_zone=>'local'); $dateObject = $oldDate->clone()->set_time_zone('UTC'); $dateObject =~ s/T/ /; print &UnixDate($dateObject,"%m/%d/%Y %H:%M")
    In order to just get the current date/time in UTC format, you can use:
    $dateObject = DateTime->now(time_zone=>'UTC'); print $dateObject;
Re: How to set a processes notion of the timezone on win32?
by jaldhar (Vicar) on Mar 02, 2010 at 18:34 UTC

    Thanks to those that replied.

    Jan "jdb" Dubois on #win32@irc.perl.org suggested a fix that works well.

    use Time::Piece; $ENV{TZ} = 'UTC'; Time::Piece::_tzset();

    According to Jan, this requires atleast Time::Piece 1.14 and is fully fixed in 1.17.

    There is also a tzset() in POSIX which I had initially considered but it gives a "not implemented" error on current strawberry perl. Hopefully that is just a bug which will soon be rectified.

    Update: Time::Piece 1.12 appears to work as well.

    --
    જલધર

      There is also a tzset() in POSIX which I had initially considered but it gives a "not implemented" error on current strawberry perl.

      Hmm, thats a very stupid bug in strawberryperl. I compiled my perl with mingw like strawberry, and my POSIX comes with tzset

Re: How to set a processes notion of the timezone on win32?
by ambrus (Abbot) on Mar 03, 2010 at 14:06 UTC
    use subs "localtime"; sub localtime { @_ ? gmtime($_[0]) : gmtime() }; + print localtime()."\n";

    Prints UTC time instead of localtime. No need to mess with timezones.

    (Or convince that module you're using to mock the time to override localtime with a fixed gmtime instead.)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://826206]
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (5)
As of 2014-12-29 05:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (184 votes), past polls