I know there is a module that does this for you such as use Time::Elapse, but where I work, adding modules is a pain, and requires getting permission from upper management. I just find it easier to create the functionality myself.

My objective – to take 2 time stamps from a file and calculate the time lapse in minutes.


#!/usr/bin/perl -w
use strict;

my $start   = "02:01:16";
my $end     = "07:12:30";
my ($startHR, $startMIN) = split(/:/, $start); # split the time by colon to separate the hour and minutes.
my $startMinutes = ($startHR*60) + $startMIN; # Convert start time to minutes
my ($endHR, $endMIN) = split(/:/, $end); # start over with end time
my $endMinutes = ($endHR*60) + $endMIN;
my $range = $endMinutes - $startMinutes; # finally subtract the two.
print $range;

The result is 311 minutes.

« »