Convert seconds to compound duration: Difference between revisions

Content added Content deleted
(→‎version 1: add Support for 0 sec (inspired by REXX Version 2)
Line 517: Line 517:
Ok! sec2str 86400 = 1 d
Ok! sec2str 86400 = 1 d
Ok! sec2str 6000000 = 9 wk, 6 d, 10 hr, 40 min</pre>
Ok! sec2str 6000000 = 9 wk, 6 d, 10 hr, 40 min</pre>

=={{header|VBScript}}==
<lang vb>
Function compound_duration(n)
Do Until n = 0
If n >= 604800 Then
wk = Int(n/604800)
n = n-(604800*wk)
compound_duration = compound_duration & wk & " wk"
End If
If n >= 86400 Then
d = Int(n/86400)
n = n-(86400*d)
If wk > 0 Then compound_duration = compound_duration & ", " End If
compound_duration = compound_duration & d & " d"
End If
If n >= 3600 Then
hr = Int(n/3600)
n = n-(3600*hr)
If d > 0 Then compound_duration = compound_duration & ", " End If
compound_duration = compound_duration & hr & " hr"
End If
If n >= 60 Then
min = Int(n/60)
n = n-(60*min)
If hr > 0 Then compound_duration = compound_duration & ", " End If
compound_duration = compound_duration & min & " min"
End If
If n > 0 Then
If min > 0 Then compound_duration = compound_duration & ", " End If
compound_duration = compound_duration & ", " & n & " sec"
n = 0
End If
Loop
End Function

'validating the function
WScript.StdOut.WriteLine compound_duration(7259)
WScript.StdOut.WriteLine compound_duration(86400)
WScript.StdOut.WriteLine compound_duration(6000000)
</lang>

{{Out}}
<pre>
2 hr, 59 sec
1 d
9 wk, 6 d, 10 hr, 40 min
</pre>


=={{header|zkl}}==
=={{header|zkl}}==