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? ;)#!/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); }
Whilst my option may not win in the efficiency stakes, I would certainly argue that it's much simpler.sub sec_to_ddmmss { my $t = shift; return $dbh->selectrow_array("SELECT sec_to_time($t);"); }
In reply to Converting seconds to DD:HH:MM:SS by McDarren
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |