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

Suppose I have these two strings:
my $beginning = 'Thu Oct 24 17:37:58 2007'; my $end = 'Fri Oct 26 06:54:09 2007';
How can I compute the time difference betweenthem (in secs)? Is there any CPAN module that does that?

I have a large text files which contain two columns (begin + end)which I need to find the time difference between them.

Replies are listed 'Best First'.
Re: Finding time difference from two strings
by graff (Chancellor) on Oct 27, 2007 at 02:12 UTC
    I'd go for Time::Local -- it's simple and direct: convert each string to "seconds since the epoch" and subtract. The only downside is having to relate month names to numbers.
    use Time::Local; my $beginning = 'Thu Oct 24 17:37:58 2007'; my $end = 'Fri Oct 26 06:54:09 2007'; my %month = ( Jan => 0, Feb => 1, Mar => 2, Apr => 3, May => 4, Jun => 5, Jul => 6, Aug => 7, Sep => 8, Oct => 9, Nov => 10, Dec => 11 ); for ( $beginning, $end ) { my ( $mo, $dy, $hr, $mi, $se, $yr ) = ( split /[\s:]/ )[1..6]; $mo = $month{$mo}; $_ = timelocal( $se, $mi, $hr, $dy, $mo, $yr ); } printf "%d sec elapsed\n", $end - $beginning;
    That's just a very crude demonstration, but it gives you an idea what needs to be done.

    update: here's a more perlish way to initialize month numbers:

    my $i=0; my %month = map {$_=>$i++} qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct +Nov Dec/;
Re: Finding time difference from two strings
by jettero (Monsignor) on Oct 27, 2007 at 02:10 UTC

    I think you're probably looking for something like Date::Manip; although, depending on how large "large" is, you might look at Time::Local. It's more work but it's a lot faster.

    -Paul

Re: Finding time difference from two strings
by thezip (Vicar) on Oct 27, 2007 at 02:31 UTC

    This will do the trick:

    (Note that I changed one of your time strings because a day name didn't agree with the other)

    Update: You'll still need to wrap into a loop such as what graff did, but you can throw mine into a sub with two time strings as parameters, and have it return the elapsed time between them.

    #!/perl/bin/perl -w use strict; use Time::Local; my %months = ( Jan => 1, Feb => 2, Mar => 3, Apr => 4, May => 5, Jun => 6, Jul => 7, Aug => 8, Sep => 9, Oct => 10, Nov => 11, Dec => 12 ); my $beginning = 'Fri Oct 26 05:54:09 2007'; my $end = 'Fri Oct 26 06:54:09 2007'; my @b = split(/[:\s]/, $beginning); my @e = split(/[:\s]/, $end); my $b = timelocal($b[5], $b[4], $b[3], $b[2], $months{$b[1]}-1, $b[-1] +); my $e = timelocal($e[5], $e[4], $e[3], $e[2], $months{$e[1]}-1, $e[-1] +); #my @new = localtime($b); #printf "%04d%02d%02d\n", $new[5]+1900, $new[4]+1, $new[3]; my $elapsed = $e - $b; print qq($elapsed seconds elapsed between the two events.\n); __OUTPUT__ 3600 seconds elapsed between the two events.

    Where do you want *them* to go today?
Re: Finding time difference from two strings
by Krambambuli (Curate) on Oct 27, 2007 at 17:11 UTC
    Here's a way to get the wanted results using Date::Manip:
    use warnings; use strict; use Data::Dumper; use Date::Manip; my $beginning = 'Thu Oct 25 17:37:58 2007'; my $end = 'Fri Oct 26 06:54:09 2007'; my $begin_date = ParseDate( $beginning ); print "From: $begin_date\n"; my $end_date = ParseDate( $end ); print "To: $end_date\n"; my $error; my $delta = DateCalc( $begin_date, $end_date, \$error); my $seconds = int( Delta_Format( $delta, 0, '%st' ) ); print "The number of seconds counted is $seconds seconds\n";
    Take care: as you gave it, the beginning date was illegal: Oct 24 was Wednesday, not Thursday. That would flag the error, so I corrected it a bit.

    Krambambuli
    ---
    enjoying Mark Jason Dominus' Higher-Order Perl
A reply falls below the community's threshold of quality. You may see it by logging in.