in reply to Convert seconds into a formatted ddd:hh:mm:ss string

This should work as well:
$T=shift; @out=reverse($T%60, ($T/=60) % 60, ($T/=60) % 24, ($T/=24) ); $out=sprintf "%03d:%02d:%02d:%02d", @out; $out=~s/^0+:|00://g; print "$out\n";


Cheers.

Replies are listed 'Best First'.
RE: RE: Convert seconds into a formatted ddd:hh:mm:ss string
by Adam (Vicar) on Aug 31, 2000 at 02:21 UTC
    Your divisions need to be integer divs, and they arn't. Other then that, I like it.

    To make the divs integer you'll need to add the line use integer; as I did. Unfortunately you can't really mix /= and int().

    Update:
    Tye is right, while you are dealing with floats, they won't have any negative effect, and the printf will convert them to ints correctly. So its all good. ++ for fundflow.

      If you want to be on the (numerically-) safe side, you can do this:
      $s=$t % 60; $t=($t-$s)/60; $m=$t % 60; $t=($t-$m)/60; $h=$t % 24; $d=($t-$h)/24; $out=sprintf "%03d:%02d:%02d:%02d\n", $d,$h,$m,$s; $out=~s/^0+:|00://g;

      Which is using only integers (assuming $t is integral)
        Well it's not really using integers .. it's still using floating point math, but the decimal portion is always zero. You and I don't care, but the computer processes 1.0 and 1 differently internally. This will make a difference when it comes to overflow and underflow conditions, and just plain execution speed ... integer math is faster, in general.

      In Perl, printf plus "%d" implies int().

              - tye (but my friends call me "Tye")
      Does that me my entry needs "use integer" as well?