Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

The opposite of localtime()

by rvosa (Curate)
on Mar 31, 2006 at 05:27 UTC ( [id://540369]=perlquestion: print w/replies, xml ) Need Help??

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");

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://540369]
Front-paged by diotalevi
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2024-04-25 19:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found