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

Given the following: "2009-03-05 18:59:11.674653" - how difficult is it in Perl to convert this into seconds since a certain date, e.g., 2000-01-01 00:00:00.000000? I suppose the tricky thing here is the date format.

Replies are listed 'Best First'.
Re: date-time into seconds
by Limbic~Region (Chancellor) on Dec 11, 2009 at 16:23 UTC
    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

      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!

Re: date-time into seconds
by zentara (Cardinal) on Dec 11, 2009 at 16:24 UTC
    .... here is this old code from someone..... it should show you how to fish
    #!/usr/bin/perl #seconds to #d_HH:MM:SS.ss and back. #constants my $m = 60; my $h = 60 * $m; my $d = 24 * $h; #example print s2dhms(86000),"\n"; print dhms2s('23:53:20'),"\n"; sub dhms2s { my $dhms = shift; my @dinfo = split /d_|:/, $dhms; my ($s, $m, $h, $d) = reverse @dinfo; $d ||= 0; $h ||= 0; $m ||= 0; return ( (( $d * 24 + $h ) * 60 + $m ) * 60 + $s ); } sub s2dhms { my $secs = shift; my $D = int($secs / $d); $secs = $secs - $D * $d; my $H = int($secs / $h); $secs = $secs - $H * $h; my $M = int($secs / $m); $secs = $secs - $M * $m; my $S = $secs ; my $r; if ($D) { $r .= sprintf "%dd_", $D; } if ( $S == int $S ) { $r .= sprintf "%02d:%02d:%02d", $H, $M, $S; } else { $r .= sprintf "%02d:%02d:%05.2f", $H, $M, $S; } return $r; }

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku
Re: date-time into seconds
by dwm042 (Priest) on Dec 11, 2009 at 20:56 UTC
    Spooky,

    The format issue goes away when you recognize that "2009-03-05 18:59:11" is a stock format that can be easily converted. The additional 0.674653 is simply a fraction of a second that can be added to an epoch date. That's at the heart of Limbic~Region's approach and every other good approach will be using some variant of this idea.

    David.