Convert seconds to compound duration: Difference between revisions

m
→‎{{header|Phix}}: updated routine with weeks, removed the now out-of-date copy that was here
(Added Wren)
m (→‎{{header|Phix}}: updated routine with weeks, removed the now out-of-date copy that was here)
Line 2,419:
 
=={{header|Phix}}==
TheThere existingis a standard function for this, for more details see builtins/pelapsed.e (which will be kept up to date, unlike giveshaving thea resultscopy shownhere)
<lang Phix>?elapsed(7259)
?elapsed(86400)
Line 2,426:
<pre>
"2 hours and 59s"
"1 day, 0 hours"
"699 weeks, 6 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.
return sprintf("%d %s%s",{v,d,iff(v=1?"":"s")})
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)
secs = sprintf(iff(integer(s)?"%ds":"%3.2fs"),s)
if m=0 then
return sprintf("%s%s",{minus,secs})
end if
s = round(s)
secs = iff(s=0?"":sprintf(" and %02ds",s))
h = floor(m/60)
m = remainder(m,60)
if h=0 then
return sprintf("%s%s%s",{minus,elapzd(m,"minute"),secs})
end if
mins = iff(m=0?"":", "&elapzd(m,"minute"))
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|PicoLisp}}==
7,830

edits