First, are you sure you've accurately transcribed the code you are running? If I run
#!/usr/bin/perl -w use strict; my $runtime = 3600; my $runtime2; my $nextruntime = 0; my $days = int($runtime / 86400); $runtime -= ($days * 86400); my $hours = int($runtime / 3600); $runtime -= ($hours * 3600); my $minutes = int($runtime / 60); my $seconds = $runtime % 60; $days = $days .'d '; $hours = $hours .'h '; $minutes = $minutes .'m '; $runtime2 = $runtime; $runtime2 = $days . $hours . $minutes . $seconds . 's'; if (time()>=$nextruntime){ $nextruntime=time()+300; $runtime += 300; print "Duration: $runtime2\n"; }
I get the output
Duration: 0d 1h 0m 0s
which corresponds with your spec but not your cited output. However, on the next iteration of your loop, I would get
Duration: 0d 0h 5m 0s
This issue happens because you are dropping hour and above information on every iteration of your loop. You need to choose if you are going to destructively parse your term, or use it as an accumulator. You'll have better luck with something like:
#!/usr/bin/perl -w use strict; my $start = time(); while (1) { my $buffer = (time() - $start); ($buffer, my $seconds) = (int $buffer/60, $buffer % 60); ($buffer, my $minutes) = (int $buffer/60, $buffer % 60); ($buffer, my $hours) = (int $buffer/24, $buffer % 24); my $days = $buffer; print "Duration: ${days}d ${hours}h ${minutes}m ${seconds}s\n"; sleep (300); }

As a side note, while(1) is incredibly abusive to system resources as you've implemented it. Take a look at sleep for implementing waiting in a script.


#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.


In reply to Re: Timestamp doesn't work well by kennethk
in thread Timestamp doesn't work well by Jabox

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.