http://qs1969.pair.com?node_id=527787

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

Hi, I need to add the following minutes: 01:45 60:00 45:25 How should I do this ? I am currently turning the minutes in seconds but I seem to be loosing some minutes

Replies are listed 'Best First'.
Re: time calculation
by rhesa (Vicar) on Feb 03, 2006 at 19:48 UTC
    What have you tried sofar?

    Converting to seconds seems like a sensible approach. You could do it like this:

    use List::Util qw/ sum /; # calculate total number of seconds $seconds = sum map { my ($m,$s) = split /:/; $m*60 + $s } qw/ 01:45 60:00 45:25 /; # or your array with times $minutes = int($seconds / 60); # integral number of minutes $seconds = $seconds % 60; # remaining seconds $new_time = sprintf "%02d:%02d", $minutes, $seconds;
Re: time calculation
by graff (Chancellor) on Feb 04, 2006 at 05:54 UTC
    Is "01:45" one minute and 45 seconds, or one hour and 45 minutes? I wonder because "60:00", if it were minutes and seconds, should probably have been "1:00:00".

    Apart from that, the next time you post a question like this, show us some code that you have tried. If all you give us is "I am currently losing some minutes", all we can say is "well, where were they when you last saw them? Have you checked all your pockets?"

    (For that matter, if you're still having trouble with this problem, you can post some code so we can help you look for those missing minutes.)

Re: time calculation
by smokemachine (Hermit) on Feb 04, 2006 at 03:39 UTC
    perl -e '($sec, $min)=localtime time; printf "%.2d:%.2d", $min, $sec'
    or
    perl -e '$,=":";print reverse @ms=(localtime)[0, 1];'