The _sensible approach seems the most sane way to me. That said, there are a few things to "fix".

First - what are the &'s doing there? Experienced perl programmer? As I said before, seeing that & is an indicator of low experience, not high experience, to me. It's an indicator of perl4 programming, which, granted, is a long time programming in perl, but does not equate to high amounts of good experience in modern perls. Unless, of course, it were used in a goto, or magic argument passing, or overriding prototypes. (Of course, the use of prototypes at all can be suspect.)

The first two options both take a parameter. Then ignore it. Your version uses your parameter. That's already a plus for your version.

And the sensible version clobbers $_. Bad. Maybe he meant to say:

local $_ = shift;
I can buy that. Except that he's not really using $_ for anything that $t wouldn't work for as a lexical variable.

Finally, the subtraction is a bit funny. You get a funny lookin' number for the -1 time (-1 day plus 23 hours plus 59 minutes plus 59 seconds - technically correct, but may not be what you want). Perhaps what he meant was "$_ = int($_ / 60)". In that case, here's a slightly revamped sensible function:

sub sec_to_dhms_sensible2 { use integer; local $_ = shift; my ($d, $h, $m, $s); $s = $_ % 60; $_ /= 60; $m = $_ % 60; $_ /= 60; $h = $_ % 24; $_ /= 24; $d = $_; return ($d, $h, $m, $s); }
I didn't fix everything, but it's a bit cleaner to look at. The only difference in output is that this gives a seconds of -1 rather than -1d, +23:59:59. Note that the use integer; is block-scoped (I confirmed this in testing) so that it doesn't affect anything else in the file - you can use floating point elsewhere.


In reply to Re: Converting seconds to DD:HH:MM:SS by Tanktalus
in thread 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.