in reply to Re^3: Time zones
in thread Time zones

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.

Replies are listed 'Best First'.
Re^5: Time zones
by sgifford (Prior) on Sep 27, 2004 at 20:18 UTC

    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.

      Okay! Since is was local and in a block that's what I tought might be happening but I wasn't really sure. Thaks for everyones patience. I ran the following code on a machine that is Eastern Time. It printed out the same time in all the print statements.
      use strict; use warnings; print scalar(localtime),"\n"; { local $ENV{'TZ'} = 'GMT'; print scalar(localtime),"\n"; local $ENV{'TZ'} = 'EST5EDT'; print scalar(localtime),"\n"; } print scalar(localtime),"\n";
        It should have printed different times right? Boy, this would be a whole lot easier the other way around. I could just call gmtime() on an EST box. Too bad I can't call estime() from a GMT!

        Yup, it did for me too. Guess you'll have to try one of the other ideas posted here. :)

        FWIW, it does work if you set TZ before starting Perl:

        $ TZ='GMT' perl -e 'print localtime()."\n"'; Mon Sep 27 21:11:30 2004 $ TZ='EST5EDT' perl -e 'print localtime()."\n"'; Mon Sep 27 17:11:38 2004 $ TZ='MST' perl -e 'print localtime()."\n"'; Mon Sep 27 14:11:50 2004
Re^5: Time zones
by ikegami (Patriarch) on Sep 27, 2004 at 20:00 UTC

    (untested)

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

    voila! 50 fixed scripts :)

Re^5: Time zones
by Anonymous Monk on Sep 27, 2004 at 20:16 UTC
    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.
      and I didn't explain myself corrently. When I said it would only affect "perl and it's children", I was refering to any given perl process. In your terminology, it will only affect your script if you only put it in your script. And if you use local, it will only affect a small region of the script, as demonstrated by the example I posted. Enjoy!
        I cut and pasted your code but got the same times for al the prints. Am I missing something?