Convert seconds to compound duration: Difference between revisions

Content added Content deleted
No edit summary
Line 1,148:
6000000 sec = 9 wk, 6 d, 10 hr, 40 min
</pre>
 
=={{header|Phix}}==
The existing standard function for this gives the results shown
<lang Phix>?elapsed(7259)
?elapsed(86400)
?elapsed(6000000)</lang>
{{out}}
<pre>
"2 hours and 59s"
"1 day, 0 hours"
"69 days, 10 hours, 40 minutes"
</pre>
The routine is implemented in builtins\pelapsed.e which you can modify as desired, and is reproduced for reference below
<lang Phix>function elapzd(integer v, string d)
-- private helper function. formats v and pluralises d by adding an "s", or not.
if v=1 then
return sprintf("%d %s",{v,d})
end if
return sprintf("%d %ss",{v,d})
end function
 
global function elapsed(atom s)
-- convert s (in seconds) into an elapsed time string suitable for display.
-- limits: a type check error occurs if s exceeds approx 100 billion years.
atom m,h,d,y
string minus = "", secs, mins
if s<0 then
minus = "minus "
s = 0-s
end if
m = floor(s/60)
s = remainder(s,60)
if integer(s) then
secs = sprintf("%ds",s)
else
secs = sprintf("%3.2fs",s)
end if
if m=0 then
return sprintf("%s%s",{minus,secs})
end if
s = round(s)
if s=0 then
secs = ""
else
secs = sprintf(" and %02ds",s)
end if
h = floor(m/60)
m = remainder(m,60)
if h=0 then
return sprintf("%s%s%s",{minus,elapzd(m,"minute"),secs})
end if
if m=0 then
mins = ""
else
mins = ", "&elapzd(m,"minute")
end if
if h<24 then
return sprintf("%s%s%s%s",{minus,elapzd(h,"hour"),mins,secs})
end if
d = floor(h/24)
h = remainder(h,24)
if d<365 then
return sprintf("%s%s, %s%s%s",{minus,elapzd(d,"day"),elapzd(h,"hour"),mins,secs})
end if
y = floor(d/365)
d = remainder(d,365)
return sprintf("%s%s, %s, %s%s%s",{minus,elapzd(y,"year"),elapzd(d,"day"),elapzd(h,"hour"),mins,secs})
end function</lang>
 
=={{header|PL/I}}==