in reply to date-time into seconds

Spooky,
There are a bunch of perl modules for this type of thing. There is new school DateTime and there is old school Date::Manip. Then there is the roll your own school (untested).
#!/usr/bin/perl use strict; use warnings; use Time::Local; my $ts1 = '2000-01-01 00:00:00.000000'; my $ts2 = '2009-03-05 18:59:11.674653'; my $elapsed = time_diff($ts1, $ts2); sub time_diff { my ($ts1, $ts2) = @_; my ($yr1, $mon1, $day1, $hr1, $min1, $sec1) = unpack('A4xA2xA2xA2x +A2xA2', $ts1); --$mon1; my $micro = substr($ts1, 19); my $epoch1 = timelocal($sec1, $min1, $hr1, $day1, $mon1, $yr1); $epoch1 += $micro; my ($yr2, $mon2, $day2, $hr2, $min2, $sec2) = unpack('A4xA2xA2xA2x +A2xA2', $ts2); --$mon2; $micro = substr($ts2, 29); my $epoch2 = timelocal($sec2, $min2, $hr2, $day2, $mon2, $yr2); $epoch2 += $micro; return $epoch2 - $epoch1; }

Cheers - L~R

Replies are listed 'Best First'.
Re^2: date-time into seconds
by wfsp (Abbot) on Dec 11, 2009 at 16:49 UTC
    Date::Time: "This module does not parse dates! ... Instead, take a look at the various DateTime::Format::* modules on CPAN."

    A suitable candidate in this case would be DateTime::Format::DateParse: "This module is a compatibility wrapper around Date::Parse."

    So just use Date::Parse. The first two lines of a three line synopsis:

    use Date::Parse; $time = str2time($date);

      Neither does Time::Local, but it didn't stop him from doing his own parsing. Do you really need a module to replace a regex match or

      my ($Y,$M,$D, $h,$m,$s) = unpack('A4xA2xA2xA2xA2xA2', $time_str);

      From that, you can contruct a DateTime object and do date and time arithmetic.

      That said, I don't see the need to replace Time::Local with DateTime here. It seems to me that L~R made appropriate and maximal use of modules. Now, if you wanted the answer to be of the form X days, Y Hours and Z Seconds, DateTime is your man!