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.


In reply to Re: increment hh:mm:ss by Aristotle
in thread increment hh:mm:ss by marauder

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.