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

Hello Folks
I was wondering whether there is a perl function to convert a time
eg:22:49:01
into its seconds equivalent. I could always multiply the first number by 60x60 and the second number by 60 alone and then add all three number up but if there was a function already written for this it would save a lot of time.
Any help would be appreciated.
Thanks.

Replies are listed 'Best First'.
Re: perl time conversion
by jeffa (Bishop) on Nov 26, 2003 at 16:19 UTC
    Time::Piece provides an interface to strptime and strftime, which i have found to be well worth learning. Here is an example that will "add" the time to today's date and print the results in a couple of different ways:
    use Time::Piece; my $time = localtime; my $date = $time->strptime($time->strftime('%D').' 22:49:01','%D %T'); print $date, "\n"; print "$_\n" for $date->min, $date->sec, $date->hour;

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: perl time conversion
by duff (Parson) on Nov 26, 2003 at 14:42 UTC
Re: perl time conversion (seconds)
by grinder (Bishop) on Nov 26, 2003 at 15:50 UTC
    I could always multiply the first number by 60x60...

    Well hey, why not?

    sub seconds { if( $_[0] =~ /^(\d+):(\d+):(\d+)$/ ) { return ($1 * 60 + $2) * 60 + $3; } else { return 0; } }

    Look how much time you just saved :) Or is there a deeper issue involved?

    update: looking at jeffa's solution underscores what I mean. Not that his solution is bad, but you have to spend the time to find, download and install the module. Then you have to memorise the % parameters. Each time I need to use strfptime, I'll have to call up the man page. For simple uses like this, that's going to be a net loss.

    There's Lazy and Lazy, and I'm not sure that the module approach is a win here. Doing what you did saved you the most time, and let you get on with the rest. It's much faster to bang out the above code (although one should not underestimate the fact that the module should have regression tests that cover what you're using it for).

      Actually I used the following subroutine. I guess I was just being lazy but when I thought about it it wasn't much trouble. Thanks monks.
      sub time_converter { if ($#_ != 0) { print "not correct amount of vars passed"; return 0; } my @timearray = split (':', $_[0]); unless ($#timearray == 2) { return 0; } return (($timearray[0]*60*60)+($timearray[1]*60)+($timearray[2])); }
Re: perl time conversion
by adrianh (Chancellor) on Nov 26, 2003 at 22:07 UTC
    I could always multiply the first number by 60x60 and the second number by 60 alone and then add all three number up but if there was a function already written for this it would save a lot of time.

    It may or may not be an issue for you, but if you're dealing with the local time remember daylight savings may muck up your calculations.