in reply to increment hh:mm:ss
my $current_time="00:02:25"; my $time_inc = 10; my ($h, $m, $s) = split(/:/, $current_time); $s += $time_inc; ### 1 ### $m += int($s / 60); ### 2 ### $s %= 60; ### 3 ### $h += int($m / 60); $m %= 60; my $new_time = join ':', $h, $m, $s; print "$new_time\n";
What we do is increase the seconds without caring about the overflow at first (see 1.) - the result may well go over 60. Then we increase the minutes by the nonfractional part of seconds / 60 (see 2.) and set the seconds to the remainder of seconds / 60 (see 3.). Since the minutes may have gone over 60 we repeat the same process there, adding the overflow to the hours. Finally we join the values with double colons and print the result.
Update: My bad. Listen to belg4mit. I would suggest a look at Time::Local instead though - since that comes bundled with Perl.
Makeshifts last the longest.
|
|---|