Convert seconds to compound duration: Difference between revisions
Content added Content deleted
m (added whitespace.) |
(adding PARI script) |
||
Line 967: | Line 967: | ||
86400 secs = 1 d, |
86400 secs = 1 d, |
||
6000000 secs = 9 wk, 6 d, 10 hr, 40 min, |
6000000 secs = 9 wk, 6 d, 10 hr, 40 min, |
||
</pre> |
|||
=={{header|PARI/GP}}== |
|||
Note: my own string function ssubstr() was used. You can find it here on RosettaCode Wiki. |
|||
{{Works with|PARI/GP|2.7.4 and above}} |
|||
<lang parigp> |
|||
\\ Convert seconds to compound duration |
|||
\\ 4/11/16 aev |
|||
secs2compdur(secs)={ |
|||
my(us=[604800,86400,3600,60,1],ut=[" wk, "," d, "," hr, "," min, "," sec"], |
|||
cd=[0,0,0,0,0],u,cdt=""); |
|||
for(i=1,5, u=secs\us[i]; if(u==0, next, cd[i]=u; secs-=us[i]*cd[i])); |
|||
for(i=1,5, if(cd[i]==0, next, cdt=Str(cdt,cd[i],ut[i]))); |
|||
if(ssubstr(cdt,#cdt-1,1)==",", cdt=ssubstr(cdt,1,#cdt-2)); |
|||
return(cdt); |
|||
} |
|||
{\\ Required tests: |
|||
print(secs2compdur(7259)); |
|||
print(secs2compdur(86400)); |
|||
print(secs2compdur(6000000)); |
|||
} |
|||
</lang> |
|||
{{Output}} |
|||
<pre> |
|||
2 hr, 59 sec |
|||
1 d |
|||
9 wk, 6 d, 10 hr, 40 min |
|||
</pre> |
</pre> |
||