in reply to Getting times (weeks, days, hours, minutes, seconds)
Actually, you weren't that far off. I avoid the decimal bits by removing the surplus seconds before the division.
For example, if you have 77 seconds, you first tell how many leftover seconds you'll get in the answer, like so: $secs = 77 % 60 giving you 17. Then you subtract those 17 seconds from your time in seconds to give you an integral multiple of 60: $t=($t-$secs)/60. You can extend it as far as you like:
#!/usr/bin/perl -w use strict; use warnings; print time_to_string(1234), "\n"; print time_to_string(12345), "\n"; print time_to_string(123456), "\n"; sub time_to_string { my $t = shift; # t=total time in seconds my $seconds = $t % 60; $t=($t-$seconds)/60; # t now has minutes my $minutes = $t % 60; $t=($t-$minutes)/60; # t now has hours my $hours = $t % 24; $t=($t-$hours) /24; # t now has days my $str = ''; $str .= "$t days, " if $t>0; $str .= sprintf "% 2u:% 2u:% 2u", $hours, $minutes, $seconds; }
Running this gives me:
roboticus@Boink:~$ ./848005.pl 0:20:34 3:25:45 1 days, 10:17:36 roboticus@Boink:~$
...roboticus
|
|---|