I am trying to add hours, minutes and seconds in the format
HH:MM:SS
and the hours and seconds seem to be adding correctly, but I am struggling to format the minutes. What I have done is convert the hours/minutes/seconds to seconds, sum them and the reconvert. Here is my code.
use strict;
use warnings;
my @total_sum = qw(10:07:03 01:01:01 02:02:02);
my ($sum, $hrs, $mins, $sec);
for my $t (@total_sum) {
my ($h, $m, $s) = split /:/, $t;
my $hm = $h*3600;
my $tm = $m*60;
$sum = $sum + $hm + $tm + $s;
}
$sec = sprintf ("%02d", $sec = $sum%60);
$mins = int($sum/60);
$hrs = int($sum/3600);
print "$hrs:$mins:$sec\n";
What I am getting is:
13:790:06 instead of
13:10:06