Convert seconds to compound duration: Difference between revisions
Content added Content deleted
(jq) |
(→{{header|jq}}: use join/1) |
||
Line 88: | Line 88: | ||
{{works with|jq|1.4}} |
{{works with|jq|1.4}} |
||
<lang jq>def seconds_to_time_string: |
<lang jq>def seconds_to_time_string: |
||
def nonzero(text): floor | if . > 0 then " |
def nonzero(text): floor | if . > 0 then "\(.) \(text)" else empty end; |
||
if . == 0 then "0 sec" |
if . == 0 then "0 sec" |
||
else |
else |
||
[(./60/60/24/7 | nonzero("wk")), |
|||
(./60/60/24 % 7 | nonzero("d")), |
|||
(./60/60 % 24 | nonzero("hr")), |
|||
(./60 % 60 | nonzero("min")), |
|||
(. % 60 | nonzero("sec"))] |
|||
| |
| join(", ") |
||
end;</lang> |
end;</lang> |
||
Line 104: | Line 104: | ||
<lang sh>$ jq -r -n -f Convert_seconds_to_compound_duration.jq |
<lang sh>$ jq -r -n -f Convert_seconds_to_compound_duration.jq |
||
0: 0 sec |
0: 0 sec |
||
7259: |
7259: 2 hr, 59 sec |
||
86400: |
86400: 1 d |
||
6000000: |
6000000: 9 wk, 6 d, 10 hr, 40 min</lang> |
||
=={{header|Perl}}== |
=={{header|Perl}}== |