[ABE.pm] time processing

Walt Mankowski waltman at pobox.com
Mon Nov 19 11:17:12 PST 2007


On Mon, Nov 19, 2007 at 01:13:52PM -0500, Ted Fiedler wrote:
> Or am I missing the boat here?

I think so.  It's not really rocket science to do these sorts of
calculations by hand.  Here's another version without any external
modules, just so you can see that it's not magic:

#!/usr/bin/perl

use strict;
use warnings;

usage() unless @ARGV == 2;

my ( $hour, $min, $sec, undef ) = split/:/, $ARGV[0];
my $distance = $ARGV[1];

my $pace = ($hour * 3600 + $min * 60 + $sec) / $distance;
printf "Pace is %02d:%02d\n", int($pace / 60), $pace % 60;

sub usage
{
    print "$0 time dist\n";
    print "  eg $0 01:54:18 13.1\n";
    exit 1;
}

The conversion to seconds is simple -- there are 60 seconds in a
minute, and 60*60=3600 seconds in an hour.  So you just multiply it
out, and divide by the distance to get the pace.

The conversion back to minutes and seconds is maybe a bit trickier.
If you divide the total seconds by 60, you get minutes.  But we don't
care about the fractional part, so I used int() to trim it off.  The
number of seconds in that minute is the remainder when we divide the
total seconds by 60.  That's what the % operator does.  (For some
reason people always seem to forget about the % operator.  It's very
useful.)

The formatting is simple with printf.  %02d formats the argument as a
2-digit number with leading 0's.

Hope this helps.

Walt


More information about the ABE-pm mailing list