http://www.perlmonks.org?node_id=659153

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

Hi,

I have tried really hard searching for a solution to this problem in many places but have yet to find a satisfactory answer, so I hope someone here can enlighten me.

Currently I have a script that is running on a remote machine (in the States, the localtime of which is the EST), I'd like certain functions to actually execute between a range of times where I am (in South Australian, our timezone is ACST).

I've considered converting the localtime of the host machine to the GMT, then converting that to our timezone (+9.5 hours) but that does not take into account issues like DST which can skew things quite a little.

It'd be great if I can get the Perl script to "sort of" run using our local timezone through some environmental variable, but even then it is still subpar if it is something that is to be employed in a persistent process. Surely there is some method or module that takes in a time specified in EST, and *poof* converts it to ACST?

Is there a more elegant way of doing it?

Another issue that I noticed is on a Solaris box here, typing the 'date' command on the command line actually yields a "CST" timezone, which can be quite ambiguous with the American "CST", is there some way to differentiate the two to perl?

Replies are listed 'Best First'.
Re: Timezone conversion in Perl
by ww (Archbishop) on Dec 27, 2007 at 06:17 UTC

    re both Q1 and Q2:check Re^2: simple locale-dependent output functions? and/or Date::Manip

    From perldoc Date::Manip (~30 lines into the synopsis):

    $date = Date_ConvTZ($date); $date = Date_ConvTZ($date,$from); $date = Date_ConvTZ($date,"",$to); $date = Date_ConvTZ($date,$from,$to);

    ie, Date::Manip does TZ conversions; I admit I have not experimented with your attempt to distinguish between CSTs

    Question 2: not even a half-baked idea... except to observe that knowing the location using CST provides at least the basis for an inference.

      I think Date::Manip is considered the slow (speedwise, not mental-capacity wise) cousin of DateTime. Either should be able to handle the fairly trivial case of timezone conversion. (What's not trivial is YOU remembering to use the same standard way of referring to time everywhere! Once you do that, adding some minutes is the easy part.)

      I am also fairly certain that any mainstream module is going to rely upon the system time's GMT offset (e.g. -0500) rather than trying to disambiguate abbreviations, so no worries there.

        Not just "considered", the Date::Manip documentation has an entire "should I use Date::Manip" section in which the modules author explains at great length that Date::Manip is by far the most flexible date/time module out there, but also the slowest (in large part because of that extreme flexibility).
Re: Timezone conversion in Perl
by polettix (Vicar) on Dec 27, 2007 at 09:50 UTC
    Timezone conversion has been covered in this issue of the 2007 Perl Advent Calendar some days ago. I don't know if it contains a solution for you, but it's probably worth investigating.

    Hey! Up to Dec 16, 2007 I was named frodo72, take note of the change! Flavio
    perl -ple'$_=reverse' <<<ti.xittelop@oivalf

    Io ho capito... ma tu che hai detto?
Re: Timezone conversion in Perl
by jasonk (Parson) on Dec 28, 2007 at 19:06 UTC

    "Surely there is some method or module that takes in a time specified in EST, and *poof* converts it to ACST?"

    There certainly is...

    use DateTime; my $time = DateTime->now( time_zone => 'America/New_York' ); $time->set_time_zone( 'Australia/South' ); print "$time\n";

    If you want to use a specific time and date rather than "now", just use new instead of now to create the object...

    my $time = DateTime->new( year => 2007, month => 12, day => 28, hour => 18, minute => 30, second => 0, time_zone => 'America/New_York', );

    "typing the 'date' command on the command line actually yields a "CST" timezone, which can be quite ambiguous with the American "CST", is there some way to differentiate the two to perl?

    Not reliably, which is why it's strongly recommended you use the named timezones rather than the abbreviations, there are quite a few abbreviations that aren't unique. On unix machines where you can't tell which one 'date' refers to, you can often tell the real timezone by looking at the file /etc/localtime (the file info, not the contents, which are binary), on my machine it looks like this:

    [jason@critter ~ 0]$ date Fri Dec 28 13:24:04 EST 2007 # oops, there are two ESTs, one at -0500 and one at +1000/+1100, which + one is it? [jason@critter ~ 0]$ ls -l /etc/localtime lrwxr-xr-x 1 root wheel 30 Dec 5 12:06 /etc/localtime@ -> /usr/sha +re/zoneinfo/US/Eastern

    We're not surrounded, we're in a target-rich environment!