我正在尝试以格式添加小时、分钟和秒
HH:MM:SS
小时和秒似乎添加正确,但我正在努力格式化分钟。我所做的是将小时/分钟/秒转换为秒,将它们相加并重新转换。这是我的代码。
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";
我得到的是:
13:790:06 instead of
13:10:06