#!/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,313942941); 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 (year) $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); } #### sub sec_to_ddmmss { my $t = shift; return $dbh->selectrow_array("SELECT sec_to_time($t);"); }