Convert seconds to compound duration: Difference between revisions

→‎{{header|REXX}}: added a 2nd REXX version.
(Tcl implementation added)
(→‎{{header|REXX}}: added a 2nd REXX version.)
Line 230:
 
=={{header|REXX}}==
===version 1===
<lang rexx>/* REXX ---------------------------------------------------------------
* Format seconds into a time string
Line 277 ⟶ 278:
9 wk, 6 d, 10 hr, 40 min</pre>
 
===version 2===
This REXX version can also handle fractional (seconds) as well as values of zero (time units).
<lang rexx>/*rexx program demonstrates how to convert a number of seconds to bigger units*/
parse arg @; if @='' then @=7259 86400 6000000 /*Not specified? Use default*/
 
do j=1 for words(@); /* [↓] process each number in the list*/
call convSec word(@,j) /*convert a number to bigger time units*/
end /*j*/
exit /*stick a fork in it, we're all done. */
/*─────────────────────────────────CONVSEC subroutine─────────────────────────*/
convSec: parse arg x /*obtain a number from the argument. */
w=timeU(60*60*24*7, 'wk' ) /*obtain number of weeks (if any). */
d=timeU(60*60*24 , 'd' ) /* " " " days " " */
h=timeU(60*60 , 'hr' ) /* " " " hours " " */
m=timeU(60 , 'min' ) /* " " " minutes " " */
s=timeU(1 , 'sec' ) /* " " " seconds " " */
if x\==0 then s=word(s 0,1)+x 'sec' /*handle fractional (residual) seconds.*/
z=strip(space(w d h m s),,','); if z=='' then z=0 'sec' /*handle zero sec.*/
say right(arg(1), 20) 'seconds: ' z
return
/*─────────────────────────────────TIMEU subroutine───────────────────────────*/
timeU: parse arg u,$; _=x%u; if _==0 then return ''; x=x-_*u; return _ $','</lang>
'''output''' when using the default inputs:
<pre>
7259 seconds: 2 hr, 59 sec
86400 seconds: 1 d
6000000 seconds: 9 wk, 6 d, 10 hr, 40 min
</pre>
'''output''' when using the inputs: &nbsp; 1800.7 &nbsp; 123.50 &nbsp; 123.00 &nbsp; 0.00
<pre>
1800.7 seconds: 30 min, 0.7 sec
123.50 seconds: 2 min, 3.50 sec
123.00 seconds: 2 min, 3 sec
0.00 seconds: 0 sec
</pre>
 
=={{header|Tcl}}==