Converts seconds into a formatted ddd:hh:mm:ss string. Nothing tricky, but I needed it. Critique welcome.
##
## We assume $in_sec is a positive integer number of seconds.
## Convert this to a 0-trimmed string of ddd:hh:mm:ss,
## or "days:hours:minutes:seconds".
##
my ($in_sec, $out_str, $out_m, $out_d, $out_h, $out_s);
$in_sec=pop @ARGV;
$hold_sec = $in_sec;
$out_d = int($in_sec / (3600*24));
$in_sec = $in_sec % (3600*24);
$out_h = int ($in_sec / 3600);
$in_sec = $in_sec % 3600;
$out_m = int ($in_sec / 60);
$in_sec = $in_sec % 60;
$out_s = $in_sec;
if ($out_d > 0) {
$out_str = sprintf "%u = %u:%0.2u:%0.2u:%0.2u\n", $hold_sec, $
+out_d, $out_h, $out_m, $out_s;
} elsif ($out_h > 0) {
$out_str = sprintf "%u = %0.2u:%0.2u:%0.2u\n", $hold_sec, $out
+_h, $out_m, $out_s;
} elsif ($out_m > 0) {
$out_str = sprintf "%u = %0.2u:%0.2u\n", $hold_sec, $out_m, $o
+ut_s;
} else {
$out_str = sprintf "%u = :%0.2u\n", $hold_sec, $out_s;
}
print $out_str;
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.