Convert seconds to compound duration: Difference between revisions
Content added Content deleted
(→{{header|REXX}}: added a 2nd REXX version.) |
Catskill549 (talk | contribs) |
||
Line 66: | Line 66: | ||
__TOC__ |
__TOC__ |
||
=={{header|AWK}}== |
|||
<lang AWK> |
|||
# syntax: GAWK -f CONVERT_SECONDS_TO_COMPOUND_DURATION.AWK |
|||
BEGIN { |
|||
n = split("7259 86400 6000000 0 1 60 3600 604799 604800 694861",arr," ") |
|||
for (i=1; i<=n; i++) { |
|||
printf("%9s %s\n",arr[i],howlong(arr[i])) |
|||
} |
|||
exit(0) |
|||
} |
|||
function howlong(seconds, n_day,n_hour,n_min,n_sec,n_week,str,x) { |
|||
if (seconds >= (x = 60*60*24*7)) { |
|||
n_week = int(seconds / x) |
|||
seconds = seconds % x |
|||
} |
|||
if (seconds >= (x = 60*60*24)) { |
|||
n_day = int(seconds / x) |
|||
seconds = seconds % x |
|||
} |
|||
if (seconds >= (x = 60*60)) { |
|||
n_hour = int(seconds / x) |
|||
seconds = seconds % x |
|||
} |
|||
if (seconds >= (x = 60)) { |
|||
n_min = int(seconds / x) |
|||
seconds = seconds % x |
|||
} |
|||
n_sec = int(seconds) |
|||
str = (n_week > 0) ? (str n_week " wk, ") : str |
|||
str = (n_day > 0) ? (str n_day " d, ") : str |
|||
str = (n_hour > 0) ? (str n_hour " hr, ") : str |
|||
str = (n_min > 0) ? (str n_min " min, ") : str |
|||
str = (n_sec > 0) ? (str n_sec " sec") : str |
|||
sub(/, $/,"",str) |
|||
return(str) |
|||
} |
|||
</lang> |
|||
<p>Output:</p> |
|||
<pre> |
|||
7259 2 hr, 59 sec |
|||
86400 1 d |
|||
6000000 9 wk, 6 d, 10 hr, 40 min |
|||
0 |
|||
1 1 sec |
|||
60 1 min |
|||
3600 1 hr |
|||
604799 6 d, 23 hr, 59 min, 59 sec |
|||
604800 1 wk |
|||
694861 1 wk, 1 d, 1 hr, 1 min, 1 sec |
|||
</pre> |
|||
=={{header|Batch File}}== |
=={{header|Batch File}}== |
||
<lang dos>@echo off |
<lang dos>@echo off |