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

Hi everyone, How do I convert CUT time to EST? ie EST is 4 hours behind CUT time and I want the exact EST in perl. Would any one be able to suggest? Thanks you

Replies are listed 'Best First'.
Re: CUT time to EST
by Zaxo (Archbishop) on May 10, 2006 at 02:40 UTC

    localtime is aware of the TZ environment variable. I'm in EDT, so a switch to EST will show up.

    $ perl -e'print scalar(localtime),$/; local $ENV{TZ} = "EST"; print sc +alar(localtime),$/;' Tue May 9 22:38:06 2006 Tue May 9 21:38:06 2006 $

    After Compline,
    Zaxo

Re: CUT time to EST
by GrandFather (Saint) on May 10, 2006 at 02:08 UTC

    Is CUT time local time, or do you want to parse a data/time string and convert it to EST? DateTime provides ways of generating a Date object and manipulating it in various ways that may do what you need.


    DWIM is Perl's answer to Gödel
      I want to parse a date/time string into EST. For example localtime() shows the CUT time which is 4 hours ahead of the EST. I want to remove the 4 hours from the localtime $hours and show the present time.

        The following code uses Date::Manip to parse a time and adjust it from AKST (Alaska Standard) to EST:

        use strict; use warnings; use Date::Manip; Date_Init ("TZ=PST"); # May not be needed and timezone irrelevant my $now = Date_ConvTZ (ParseDate ('00:00:00'), 'AKST', 'EST'); print UnixDate ($now, '%H:%M:%S');

        Prints:

        04:00:00

        DWIM is Perl's answer to Gödel
Re: CUT time to EST
by gam3 (Curate) on May 10, 2006 at 02:31 UTC
    Here is the way I would do it:
    { local $ENV{TZ} = 'EST'; print scalar localtime(), "\n"; }
    This is UNIX specific and you might replace EST with Canada/Eastern or some other name so EST or EDT will be used as required.
    Another approach that is more portable, but only works it both timezones use the same meathod for day light savings is
    print scalar localtime(time - 4 * 60 * 60), "\n";
    -- gam3
    A picture is worth a thousand words, but takes 200K.