Hi all,

Recently I found myself having to convert an integer value (representing a number of seconds) into DD:HH:MM:SS

After messing around with time, localtime and a few of the Date/Time manipulation modules for a while - I couldn't see any quick and easy way to do it. And by "quick and easy" I mean a simple one line call to a function or method.

So I asked a work colleague of mine (a very experienced Perl programmer) how he would do it, and he quickly came up with the following options:
#!/usr/bin/perl -w use strict; use Date::Calc qw(Delta_DHMS); my ($d, $h, $m, $s); my @secs = (1000,10000,100000,1000000,86400,86399,3600,3599,0,-1,31394 +2941); for (@secs) { ($d, $h, $m, $s) = &sec_to_dhms_silly($_); print "$d days, $h hours, $m minutes, $s seconds\n"; ($d, $h, $m, $s) = &sec_to_dhms_sensible($_); print "$d days, $h hours, $m minutes, $s seconds\n"; } sub sec_to_dhms_silly { my @epoch = (1970, 1, 1, 0,0,0); my @mytime = gmtime(shift); splice (@mytime, 6); # discard fields after 6th element (yea +r) $mytime[5] += 1900; # gmtime returns year - 1900 $mytime[4]++; # gmtime has zero based month @mytime = reverse @mytime; # Delta_DHMS expects args in reverse to + gmtime return Delta_DHMS(@epoch, @mytime); } sub sec_to_dhms_sensible { shift; my ($d, $h, $m, $s); $s = $_ % 60; $_ = ($_ - $s) / 60; $m = $_ % 60; $_ = ($_ - $m) / 60; $h = $_ % 24; $_ = ($_ - $h) / 24; $d = $_; return ($d, $h, $m, $s); }
In the meantime, I came up with my own solution (and I'm sure I'll get beaten up bigtime for this - but hey, I already had the DB handle open - so why not make use of it? ;)
sub sec_to_ddmmss { my $t = shift; return $dbh->selectrow_array("SELECT sec_to_time($t);"); }
Whilst my option may not win in the efficiency stakes, I would certainly argue that it's much simpler.

And so my question is: Is there a simpler way than any of the above?
I guess what I'm looking for is a module that supports a method similar to the mySQL SEC_TO_TIME function - but it doesn't seem to exist?

Thanks,
Update:Just to clarify - the number of seconds represents "time remaining", and it's not relative to any particular point in time. So it's not a calculation - merely a straight conversion (if that makes sense ;)

--Darren :)

In reply to Converting seconds to DD:HH:MM:SS by McDarren

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.