in reply to Re: Time zones
in thread Time zones

That's a really neat trick. I can't use it though. Other applications and scripts run on the box that mine does. If I modify the environment, I'll mess up the other programs.

Replies are listed 'Best First'.
Re^3: Time zones
by ikegami (Patriarch) on Sep 27, 2004 at 19:35 UTC

    In any case, changing %ENV only affects perl and it's children, so your other applications and users won't be affected. (Note: perl is in the same process as Apache in mod_perl, so it would probably affect it a bit more in that scenario.)

    But you use local to tightly scope the change if you were so inclined:

    print scalar(localtime),"\n"; { local $ENV{'TZ'} = 'AST4ADT'; print scalar(localtime),"\n"; } print scalar(localtime),"\n"; __END__ output ====== Mon Sep 27 15:43:45 2004 Mon Sep 27 16:43:45 2004 Mon Sep 27 15:43:45 2004
      This code won't work on all systems. The C library may cache the timezone information between calls to localtime(3). If you set the TZ environment variable, you need to call tzset(3), which is avilable in the POSIX Perl module.
      use POSIX; print scalar(localtime),"\n"; { local $ENV{'TZ'} = 'AST4ADT'; POSIX::tzset; print scalar(localtime),"\n"; } POSIX::tzset; print scalar(localtime),"\n";
      Okay, thanks. I still have a problem though. The box it runs on also runs 50 or more other Perl scrips so I think I'm still out of luck.

        Assuming you're not running under mod_perl, I think you may have misunderstood what ikegami meant by changing %ENV only affects perl and it's children. This change will only affect the process you change it in, and any processes started by that process, with a call to fork or the equivalent. Even under mod_perl, I suspect it will work OK if you localize it.

        Changing %ENV does not affect any other programs on the system, whether they are Perl scripts or otherwise. It doesn't even affect other running copies of the same script.

        (untested)

        perl -i.bak -pe '$. == 2 && print("BEGIN { \$ENV{\"TZ\"}=\"EST5EDT\"; +}\n");' `find . -name "*.pl"`

        voila! 50 fixed scripts :)

        I didn't explain it correctly. The problem isn't that I'd have to modify 50 scripts for the new zone. The problem is that modifing the TZ environment variable might affect those scripts. You said that TZ only effects the Perl scrips and their children. I'm not sure if localizing the TZ environment variable like you did would olny make the varibale accessable by the local script (the script with the code in it) or if the others would pick it up. I only want my script to see it.