Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks. If I'm using a machine that is on GMT time, how can I convert that time to EST using a standard Perl 5.6.1 installation? That is, without downloading Date::Calc from CPAN. I can't simply add 5 hours to GMT time because daylight savings time isn't always the same between zones. Also, how can I constuct an EST time when the default is GMT. I need to compare the curret time which is in a GMT zone to a hard-coded EST time. Based on the results of this comparison, the code will take different paths.

Replies are listed 'Best First'.
Re: Time zones
by idsfa (Vicar) on Sep 27, 2004 at 19:15 UTC

    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.
      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
Re: Time zones
by jonadab (Parson) on Sep 27, 2004 at 19:16 UTC

    All the *good* answers to this question are going to involve some module or another. Otherwise, you're going to have to basically duplicate the (relevant) functionality of the module in your code.

    The module doesn't have to be Date::Calc necessarily. There are numerous other options. (I would use DateTime myself.) You *may* even be able to find one that's pure Perl and doesn't have a lot of dependencies, so that you can just place a copy of the .pm in the same directory as your code and not have to install it per se. (DateTime is not the answer in this respect; it has a trainload of dependencies; the reason I use it is because it does everything. The DateTime::Format modules are particularly useful. But that stuff is beyond your needs just at the moment.) Poke around on search.cpan.org with search strings like "date" and "time" and "timezone" and see if you can find a _simple_ module that's pure perl and just does what you want.


    "In adjectives, with the addition of inflectional endings, a changeable long vowel (Qamets or Tsere) in an open, propretonic syllable will reduce to Vocal Shewa. This type of change occurs when the open, pretonic syllable of the masculine singular adjective becomes propretonic with the addition of inflectional endings."  — Pratico & Van Pelt, BBHG, p68
Re: Time zones
by Grygonos (Chaplain) on Sep 27, 2004 at 19:18 UTC

    I don't know of a way w/o Date::Calc, however depending on what you are doing you may not need perl. If you are doing something as simple as taking the users local time and displaying a msg like good morning, good afternoon, good evening, etc. That is better left to stuff like Javascript. However if it's not (which i suspect it isn't) as simple as that then i'm inclined to ask why you can't install Date::Calc? just restrictions or not wanting to fool with it?

      Thanks Grygonos. I need to do it in Perl. Based on a time, my script needs to do differnt things. I can't install CPAN modules becuase the environment that this code is in must use Perl 5.6.1 with no other modules.
Re: Time zones
by DrHyde (Prior) on Sep 28, 2004 at 09:05 UTC
    Actually, GMT and US Eastern Standard Time (I assume that's the EST you mean, given that you mention adding 5 hours) *are* always 5 hours apart. What differs is that dates on which the respective places switch between GMT/BST and EST/EDT.</pedant>
        If you care about the time difference between two places, then ask for that. Instead, you asked:

        If I'm using a machine that is on GMT time, how can I convert that time to EST using a standard Perl 5.6.1 installation?

        And the answer is "subtract five hours". Always.

          A reply falls below the community's threshold of quality. You may see it by logging in.