in reply to Re: String (date) to time (integer)
in thread String (date) to time (integer)

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.

Replies are listed 'Best First'.
Re^3: String (date) to time (integer)
by northwestdev (Acolyte) on Jun 06, 2009 at 15:01 UTC
    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.