Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

TimeZone convert problem.

by hmb104 (Sexton)
on Oct 04, 2012 at 19:39 UTC ( #997304=perlquestion: print w/replies, xml ) Need Help??
hmb104 has asked for the wisdom of the Perl Monks concerning the following question:

Hello Perl people,

I need help converting between times zone. My DHCP server uses Central time zone, and I got to search different time zones such as GMT, UTC, EST .. etc.

Is there a cpan module can help me with that? Also how I can convert the time format such as "2012-10-02T12:34:08.94Z" to a UNIX format?

Thanks for the help.

Replies are listed 'Best First'.
Re: TimeZone convert problem.
by Anonymous Monk on Oct 04, 2012 at 19:45 UTC
        Start with one of these
        my $dt = DateTime::Format::ISO8601->parse_datetime( $datestr ); my $dt = DateTime::Format::DateParse->parse_datetime( $datestr ); my $dt = DateTime::Format::Flexible->parse_datetime( $datestr );
        Then finish with
        $dt->set_time_zone( 'Country/City' ); print $dt->strftime( q[%a %b %e %H:%M:%S %Z %Y] ), "\n";
        or whatever
Re: TimeZone convert problem.
by kennethk (Abbot) on Oct 04, 2012 at 19:45 UTC
    My usual goto for dealing with time is DateTime. I don't know about the particular string format you are listing, but chances are there is something to do it in the DateTime::Format namespace, as dscussed in Parsing Dates.

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

      Format is ISO 8601 ("T" is the separator between date & time; "z" is UTC time zone).
Re: TimeZone convert problem.
by DrHyde (Prior) on Oct 05, 2012 at 11:28 UTC

    DateTime is the canonical module for handling dates and times. DateTime::Format::ISO8601 and various other DateTime::Formats are handy for parsing textual dates and times.

    But you don't want to search for timezone abbreviations. Really, you don't. To take just one example, when you say EST do you mean UTC-5 (North America), UTC-3 (Brazil), or UTC+10 (Australia)?

Re: TimeZone convert problem.
by Marshall (Abbot) on Oct 05, 2012 at 02:02 UTC
    how I can convert the time format such as "2012-10-02T12:34:08.94Z" to a UNIX format?

    You cannot do that because the Unix time format is an integer seconds from the "epoch time". ".94" hundredth's seconds is meaningless.

    A conversion from "2012-10-02 12:34:08Z" to a Unix time_t integer is possible.

    It is possible to track higher resolutions that one second. But you need a different data structure than a time_t value.

    I would look at Date::Time.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://997304]
Front-paged by Corion
and John Coltrane plays...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2017-07-20 18:45 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (312 votes). Check out past polls.