in reply to How do I convert seconds into a readable time?

My version is slightly different, and does not quite work right. Could someone spot my mistake?
It's not written in perl, but the language is basic enough to be portable pretty much anywhere

function convertToTime(seconds) var thex var they var thez var mind mind = Int(seconds) thex = mind Mod 60 if thex = 0 thex = mind / 60 else mind = (int(mind ) - int(thex)) end they = mind Mod 3600 if they = 0 they = mind / 3600 else mind = mind - they end thez = mind / 60 theTime = thez/60 + ":" + they/60 + ":" + thex return theTime end

Replies are listed 'Best First'.
Re: Answer: How do I convert seconds into a readable time?
by zengargoyle (Deacon) on Oct 21, 2003 at 15:30 UTC

    seconds to #d_HH:MM:SS.ss and back.

    sub dhms2s { my $dhms = shift; my @dinfo = split /d_|:/, $dhms; my ($s, $m, $h, $d) = reverse @dinfo; $d ||= 0; $h ||= 0; $m ||= 0; return ( (( $d * 24 + $h ) * 60 + $m ) * 60 + $s ); } my $m = 60; my $h = 60 * $m; my $d = 24 * $h; sub s2dhms { my $secs = shift; my $D = int($secs / $d); $secs = $secs - $D * $d; my $H = int($secs / $h); $secs = $secs - $H * $h; my $M = int($secs / $m); $secs = $secs - $M * $m; my $S = $secs ; my $r; if ($D) { $r .= sprintf "%dd_", $D; } if ( $S == int $S ) { $r .= sprintf "%02d:%02d:%02d", $H, $M, $S; } else { $r .= sprintf "%02d:%02d:%05.2f", $H, $M, $S; } return $r; }