Convert seconds to compound duration: Difference between revisions

Content added Content deleted
(adding PARI script)
m (→‎version 2: added/changed comments and whitespace.)
Line 1,379: Line 1,379:
===version 2===
===version 2===
This REXX version can also handle fractional (seconds) as well as values of zero (time units).
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*/
<lang rexx>/*rexx program demonstrates how to convert a number of seconds to bigger time units.*/
parse arg @; if @='' then @=7259 86400 6000000 /*Not specified? Use default*/
parse arg @; if @='' then @=7259 86400 6000000 /*Not specified? Then use the default.*/


do j=1 for words(@); /* [↓] process each number in the list*/
do j=1 for words(@); /* [↓] process each number in the list*/
call convSec word(@,j) /*convert a number to bigger time units*/
call convSec word(@, j) /*convert a number to bigger time units*/
end /*j*/
end /*j*/
exit /*stick a fork in it, we're all done. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*─────────────────────────────────CONVSEC subroutine─────────────────────────*/
convSec: parse arg x /*obtain a number from the argument. */
convSec: parse arg x /*obtain a number from the argument. */
w=timeU(60*60*24*7, 'wk' ) /*obtain number of weeks (if any). */
w=timeU( 60*60*24*7, 'wk' ) /*obtain number of weeks (if any). */
d=timeU(60*60*24 , 'd' ) /* " " " days " " */
d=timeU( 60*60*24 , 'd' ) /* " " " days " " */
h=timeU(60*60 , 'hr' ) /* " " " hours " " */
h=timeU( 60*60 , 'hr' ) /* " " " hours " " */
m=timeU(60 , 'min' ) /* " " " minutes " " */
m=timeU( 60 , 'min' ) /* " " " minutes " " */
s=timeU(1 , 'sec' ) /* " " " seconds " " */
s=timeU( 1 , 'sec' ) /* " " " seconds " " */
if x\==0 then s=word(s 0,1)+x 'sec' /*handle fractional (residual) 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.*/
z=strip(space(w d h m s),,","); if z=='' then z=0 "sec" /*handle 0 sec.*/
say right(arg(1), 20) 'seconds: ' z
say right(arg(1), 20) 'seconds: ' z
return
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*─────────────────────────────────TIMEU subroutine───────────────────────────*/
timeU: parse arg u,$; _=x%u; if _==0 then return ''; x=x-_*u; return _ $','</lang>
timeU: parse arg u,$; _=x%u; if _==0 then return ''; x=x-_*u; return _ $","
</lang>
'''output''' when using the default inputs:
'''output''' &nbsp; when using the default (internal) inputs:
<pre>
<pre>
7259 seconds: 2 hr, 59 sec
7259 seconds: 2 hr, 59 sec
Line 1,405: Line 1,406:
6000000 seconds: 9 wk, 6 d, 10 hr, 40 min
6000000 seconds: 9 wk, 6 d, 10 hr, 40 min
</pre>
</pre>
'''output''' when using the inputs: &nbsp; 1800.7 &nbsp; 123.50 &nbsp; 123.00 &nbsp; 0.00
'''output''' &nbsp; when using the inputs: &nbsp; <tt> &nbsp; 1800.7 &nbsp; 123.50 &nbsp; 123.00 &nbsp; 0.00 </tt>
<pre>
<pre>
1800.7 seconds: 30 min, 0.7 sec
1800.7 seconds: 30 min, 0.7 sec