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:
I can buy that. Except that he's not really using $_ for anything that $t wouldn't work for as a lexical variable.local $_ = shift;
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:
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.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); }
In reply to Re: Converting seconds to DD:HH:MM:SS
by Tanktalus
in thread Converting seconds to DD:HH:MM:SS
by McDarren
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |