Convert seconds to compound duration: Difference between revisions

Content added Content deleted
m (→‎{{header|Phix}}: another iff)
m (→‎version 2: changed subroutine into a function, added whitespace to the output.)
Line 1,536: Line 1,536:
parse arg @; if @='' then @=7259 86400 6000000 /*Not specified? Then use the 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(@); z=word(@, j) /* [↓] process each number in the list*/
call convSec word(@, j) /*convert a number to bigger time units*/
say right(z, 25) 'seconds: ' convSec(z) /*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: 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 0 sec.*/
$=strip(space(w d h m s),,","); if $=='' then z=0 "sec" /*handle 0 sec.*/
return $
say right(arg(1), 20) 'seconds: ' z
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
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''' &nbsp; when using the default (internal) inputs:
'''output''' &nbsp; when using the default (internal) inputs:
<pre>
<pre>
7259 seconds: 2 hr, 59 sec
7259 seconds: 2 hr, 59 sec
86400 seconds: 1 d
86400 seconds: 1 d
6000000 seconds: 9 wk, 6 d, 10 hr, 40 min
6000000 seconds: 9 wk, 6 d, 10 hr, 40 min
</pre>
</pre>
'''output''' &nbsp; when using the inputs: &nbsp; <tt> &nbsp; 1800.7 &nbsp; 123.50 &nbsp; 123.00 &nbsp; 0.00 </tt>
'''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
123.50 seconds: 2 min, 3.50 sec
123.50 seconds: 2 min, 3.50 sec
123.00 seconds: 2 min, 3 sec
123.00 seconds: 2 min, 3 sec
0.00 seconds: 0 sec
0.00 seconds: 0 sec
</pre>
</pre>