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

Dear monks,

Let's say I have a date / time parsed out in a bunch of variables:
if( $time =~ m/^(\d{4})(\d{2})(\d{2})T(\d{2})(\d{2})(\d{2})(Z?)$/ ) { ( $year, $month, $day, $hour, $min, $sec, $is_gmt ) = ( $1, $2, $3, $4, $5, $6, $7 ); }
Is there a shortcut to turn these into a value as returned by time(), i.e. number of seconds since the epoch? So that I can do numerical comparisons on the dates, and then format them back into something pretty using localtime() or the POSIX strftime function?

Thanks!

Replies are listed 'Best First'.
Re: The opposite of localtime()
by brian_d_foy (Abbot) on Mar 31, 2006 at 05:36 UTC

    There are a number of ways that you can do this. The DateTime module can do it (as can the other Date modules), and POSIX has the mktime function. The Time::Local module has the timelocal (so, backwards in name too), which shows up in the Dates section of perlfaq4.

    --
    brian d foy <brian@stonehenge.com>
    Subscribe to The Perl Review
      I checked out timelocal from Time::Local. It does exactly what I was looking for, including even the reversed name :-)

      Thank you all for your responses! I found what I needed.
Re: The opposite of localtime()
by xdg (Monsignor) on Mar 31, 2006 at 05:35 UTC

    See POSIX::mktime().

    > perl -mPOSIX -le "print time; @t = localtime; print qq{@t}; print P +OSIX::mktime(@t)" 1143783264 24 34 0 31 2 106 5 89 0 1143783264

    -xdg

    Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Re: The opposite of localtime()
by gube (Parson) on Mar 31, 2006 at 06:20 UTC

    Hi,

    Time::Piece is also a easy module. you can refer Time::Piece module for the localtime..It's an Object Oriented time objects. From the object you can get the year, month , day and also using the strftime you can get any format of localtime. Refer the sample code given below..

    use Time::Piece; my $t = localtime; print "Time is $t\n"; print "Year is ", $t->year, "\n"; print $t->strftime("%a, %d %b %Y");