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(@); |
do j=1 for words(@); z=word(@, j) /* [↓] process each number in the list*/ |
||
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' ) |
w=timeU( 60*60*24*7, 'wk' ) /*obtain number of weeks (if any). */ |
||
d=timeU( 60*60*24 , 'd' ) |
d=timeU( 60*60*24 , 'd' ) /* " " " days " " */ |
||
h=timeU( 60*60 , 'hr' ) |
h=timeU( 60*60 , 'hr' ) /* " " " hours " " */ |
||
m=timeU( 60 , 'min' ) |
m=timeU( 60 , 'min' ) /* " " " minutes " " */ |
||
s=timeU( 1 , 'sec' ) |
s=timeU( 1 , 'sec' ) /* " " " seconds " " */ |
||
if x\==0 then s=word(s 0,1)+x 'sec' |
if x\==0 then s=word(s 0, 1) + x 'sec' /*handle fractional (residual) seconds.*/ |
||
$=strip(space(w d h m s),,","); if $=='' then z=0 "sec" /*handle 0 sec.*/ |
|||
⚫ | |||
say right(arg(1), 20) 'seconds: ' z |
|||
⚫ | |||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
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 (internal) inputs: |
'''output''' 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''' when using the inputs: <tt> 1800.7 123.50 123.00 0.00 </tt> |
'''output''' when using the inputs: <tt> 1800.7 123.50 123.00 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> |
||