Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

ENV{TZ} and localtime? (inconsistent time in child script)

by blahblahblah (Priest)
on Sep 06, 2007 at 15:11 UTC ( #637449=perlquestion: print w/ replies, xml ) Need Help??
blahblahblah has asked for the wisdom of the Perl Monks concerning the following question:

I recently had to add some code that sets $ENV{TZ} to the correct local time. (Date::Manip requires this, and it wasn't already in my ENV on windows.) I've noticed that since doing that, when I call one perl script from another the localtime incorrectly returns GMT in the called script.
$ENV{'TZ'} = '-0400'; # also tried 'US/Eastern' with same result. print qq{---- $ENV{'COUNT'} ---- TZ: $ENV{'TZ'} localtime: } . scalar(localtime) . "\n"; $ENV{'COUNT'}++; exit if $ENV{'COUNT'} > 3; system("C:\\perl\\bin\\perl.exe C:\\cgi\\test");
The result is:
C:\FootPrints1\cgi>C:\perl\bin\perl.exe C:\cgi\test ---- ---- TZ: -0400 localtime: Thu Sep 6 10:46:07 2007 ---- 1 ---- TZ: -0400 localtime: Thu Sep 6 14:46:07 2007 ---- 2 ---- TZ: -0400 localtime: Thu Sep 6 14:46:07 2007 ---- 3 ---- TZ: -0400 localtime: Thu Sep 6 14:46:07 2007
I'm using ActiveState 5.8.8. When I try the same on Solaris with 5.8.1 it always shows the correct local time. I tried the following code after setting ENV{TZ}, but it had no effect on the result:
use POSIX(); POSIX::tzset();
Any ideas how I can make localtime give me the local time (while still setting ENV{TZ} to keep Date::Manip happy)? What settings does localtime depend on?

Thanks, Joe

Comment on ENV{TZ} and localtime? (inconsistent time in child script)
Select or Download Code
Replies are listed 'Best First'.
Re: ENV{TZ} and localtime? (inconsistent time in child script)
by ikegami (Pope) on Sep 06, 2007 at 16:54 UTC

    I started looking into this, but I hit the limit of my currently available resources. I hope to look further into this tonight. Here's what I found out so far:

    • POSIX::tzset is a thin wrapper for the C library function with the same name. The entirety of the XS code is:

      void tzset()
    • Windows and Visual Studio support tzset.

    • Changes to %ENV are reflected in the process's actual environment. I used Win32::ExpandEnvironmentStrings (which is mapped to a system call that knows nothing of %ENV) to verify this.

    I hope to test the example code in MSDN's tzset documentation using VS6, what ActivePerl uses.

      Thanks, I think I found an solution in the docs you referenced for _tzset:
      Use the following syntax to set the TZ environment variable: set TZ=tzn[+ | ]hh[:mm[:ss] ][dzn] tzn Three-letter time-zone name, such as PST. You must specify the correct + offset from local time to UTC.
      So I tried "EST5EDT" instead of -0400, and that gives the right result. I'm still not sure why it behaves differently when called via system() though.

      update:used code tags so brackets in syntax aren't hidden.

Re: ENV{TZ} and localtime? (inconsistent time in child script)
by gam3 (Curate) on Sep 06, 2007 at 15:51 UTC
    Works on linux. I assume that TZ must be being set when perl is run. One thing that you could do is run perl from a batch file and set TZ in that.
    -- gam3
    A picture is worth a thousand words, but takes 200K.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (21)
As of 2015-07-28 14:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (255 votes), past polls