in reply to Time zones

You can use the TZ Environment Variable to do this ... (detailed at the bottom of the linked page)

$ perl -e '$ENV{TZ}="EST5EDT"; print scalar(localtime),"\n";' Mon Sep 27 15:13:13 2004 $ perl -e '$ENV{TZ}="GMT"; print scalar(localtime),"\n";' Mon Sep 27 19:13:22 2004

If anyone needs me I'll be in the Angry Dome.

Replies are listed 'Best First'.
Re^2: Time zones
by Anonymous Monk on Sep 27, 2004 at 19:31 UTC
    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.

      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.