Skip to content

Instantly share code, notes, and snippets.

@justingit
Last active August 11, 2016 05:23
Show Gist options
  • Save justingit/7e2cc720faf23c8070026924285efff2 to your computer and use it in GitHub Desktop.
Save justingit/7e2cc720faf23c8070026924285efff2 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use DateTime;
my $start_str = '05:11:43 AM (MDT) 07/26/16';
my $end_str = '02:58:07 PM (MDT) 08/10/16';
my $num_14ers = 27;
my $start = parse_trackleaders_str($start_str);
my $end = parse_trackleaders_str($end_str);
my $start_dt = DateTime->new($start);
my $end_dt = DateTime->new($end);
my $dur = $start_dt->subtract_datetime_absolute($end_dt);
my $per_days_sec = int( $dur->seconds ) / $num_14ers;
print formatted_runtime( $dur->seconds ) . "\n";
print 'Summiting a 14er every, ' . formatted_runtime($per_days_sec) . "\n";
print sprintf( "%.2f", ( 86400 / $per_days_sec ) ) . ' 14ers/day';
sub parse_trackleaders_str {
my $str = shift;
my $d = {};
( $d->{time}, $d->{tod}, $d->{time_zone}, $d->{date} ) =
split( ' ', $str, 4 );
( $d->{hour}, $d->{minute}, $d->{second} ) = split( ':', $d->{time}, 3 );
( $d->{month}, $d->{day}, $d->{year} ) = split( '/', $d->{date}, 3 );
if ( $d->{tod} eq 'PM' ) {
$d->{hour} = int( $d->{hour} ) + 12;
}
#$d->{time_zone} =~ s/\(|\)//g;
$d->{time_zone} = 'local'; # uh, hack. I'm in Colorado!
delete( $d->{date} );
delete( $d->{time} );
delete( $d->{tod} );
return $d;
}
sub formatted_runtime {
my $d = shift || 0;
my @int = (
[ 'second', 1 ],
[ 'minute', 60 ],
[ 'hour', 60 * 60 ],
[ 'day', 60 * 60 * 24 ],
[ 'week', 60 * 60 * 24 * 7 ],
[ 'month', 60 * 60 * 24 * 30.5 ],
[ 'year', 60 * 60 * 24 * 30.5 * 12 ]
);
my $i = $#int;
my @r;
while ( ( $i >= 0 ) && ($d) ) {
if ( $d / $int[$i]->[1] >= 1 ) {
push @r, sprintf "%d %s%s", $d / $int[$i]->[1],
$int[$i]->[0], ( sprintf "%d", $d / $int[$i]->[1] ) > 1
? 's'
: '';
}
$d %= $int[$i]->[1];
$i--;
}
my $runtime;
if (@r) {
$runtime = join ", ", @r;
}
else {
$runtime = '0 seconds';
}
return $runtime;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment