Lady Aleena:

Actually, you weren't that far off. I avoid the decimal bits by removing the surplus seconds before the division.

For example, if you have 77 seconds, you first tell how many leftover seconds you'll get in the answer, like so: $secs = 77 % 60 giving you 17. Then you subtract those 17 seconds from your time in seconds to give you an integral multiple of 60: $t=($t-$secs)/60. You can extend it as far as you like:

#!/usr/bin/perl -w use strict; use warnings; print time_to_string(1234), "\n"; print time_to_string(12345), "\n"; print time_to_string(123456), "\n"; sub time_to_string { my $t = shift; # t=total time in seconds my $seconds = $t % 60; $t=($t-$seconds)/60; # t now has minutes my $minutes = $t % 60; $t=($t-$minutes)/60; # t now has hours my $hours = $t % 24; $t=($t-$hours) /24; # t now has days my $str = ''; $str .= "$t days, " if $t>0; $str .= sprintf "% 2u:% 2u:% 2u", $hours, $minutes, $seconds; }

Running this gives me:

roboticus@Boink:~$ ./848005.pl 0:20:34 3:25:45 1 days, 10:17:36 roboticus@Boink:~$

...roboticus


In reply to Re: Getting times (weeks, days, hours, minutes, seconds) by roboticus
in thread Getting times (weeks, days, hours, minutes, seconds) by Lady_Aleena

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.