Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: String (date) to time (integer)

by liverpole (Monsignor)
on Jun 06, 2009 at 14:01 UTC ( [id://769094]=note: print w/replies, xml ) Need Help??


in reply to String (date) to time (integer)

Hi northwestdev,

Use Time::Local for the function timelocal, which is the inverse of the builtin localtime.

For example:

use strict; use warnings; use Time::Local; my $date = "06/06/2009"; my ($m,$d,$y) = $date =~ m|(\d+)/(\d+)/(\d+)|; my $timet = timelocal(0, 0, 0, $d, $m-1, $y); print "Date '$date' => $timet\n" # Updated -- check the reverse my $ltime = localtime($timet); print "$timet => $ltime\n"; # Results: # Date '06/06/2009' => 1244260800 # 1244260800 => Sat Jun 6 00:00:00 2009

Note that the first three arguments to timelocal above represent (respectively) the number of seconds, minutes, and hours; in your case they can be zero, as you're only interested in a one-day granularity.

Update:  As Corion reminds me, the month passed to timelocal needs to be zero-based, not one-based the way humans represent dates.  Changing "$m" to "$m-1" in my code fixed this (thanks, Corion!).


s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/

Replies are listed 'Best First'.
Re^2: String (date) to time (integer)
by Corion (Patriarch) on Jun 06, 2009 at 14:30 UTC

    Actually not exactly:

    use strict; use Time::Local; for my $date ("06/06/2009",'01/30/09') { my ($m,$d,$y) = $date =~ m|(\d+)/(\d+)/(\d+)|; my $timet = timelocal(0, 0, 0, $d, $m, $y); print "Date '$date' => $timet\n"; print "localtime($timet): ", scalar localtime $timet, "\n"; } __END__ Date '06/06/2009' => 1246831200 localtime(1246831200): Mon Jul 6 00:00:00 2009 Day '30' out of range 1..28 at .pl line 6

    As the Time::Local documentation says:

    The value for the day of the month is the actual day (ie 1..31), while the month is the number of months since January (0..11).

    So you need to pass in $m-1 to adjust for that.

      Thank you. I am new to Perl, so I am not clear on how to decipher ("06/06/2009",'01/30/09').

        Actually, the relevant part is

        for my $date ("06/06/2009",'01/30/09') { ...

        which is a loop that loops over the two elements 06/06/2009 and 01/30/09. I used a loop to demonstrate the original case and a problematic case, because timelocal(...,30,1,9) would try to find the epoch time of the 30th of February, which doesn't exist in our calendar.

Re^2: String (date) to time (integer)
by dsheroh (Monsignor) on Jun 07, 2009 at 13:59 UTC
    Personally, I tend to see Time::Local as "the hard way" (due in part to the 0-based/1-based issue) and prefer DateTime::Format::Strptime:
    #!/usr/bin/perl use strict; use warnings; use DateTime::Format::Strptime; my $date_parser = DateTime::Format::Strptime->new(pattern => '%D'); while (<DATA>) { print $date_parser->parse_datetime($_)->epoch, "\n"; } __DATA__ 06/06/2009 01/30/2009
    This example prints the epoch values for each date, as the OP said he wanted to convert them to an integer (and epoch is what the time function returns), but the DateTime suite also includes tools for displaying the date in pretty much whatever format you may want if epoch isn't what you're looking for.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://769094]
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found