Convert seconds to compound duration: Difference between revisions

 
(12 intermediate revisions by 4 users not shown)
Line 1,555:
=={{header|EasyLang}}==
<syntaxhighlight lang="text">
procfunc$ split sec . s$ .
divs[] = [ 60 60 24 7 ]
n$[] = [ "sec" "min" "hr" "d" "wk" ]
len r[] 5
for i = 1 to 4
r[i] = sec mod divs[i]
sec = sec div divs[i]
.
r[5] = sec
s$ for i = ""5 downto 1
for i = 5 downtoif r[i] <> 10
if r[i]s$ <> 0""
if s$ <>&= ", "
s$ &= ", ".
s$ &= r[i] & " " & n$[i]
.
.
s$ &= r[i] & " " & n$[i]
return .s$
.
.
callprint split 7259 s$
print s$split 86400
callprint split 86400 s$6000000
print s$
call split 6000000 s$
print s$
</syntaxhighlight>
 
Line 2,549 ⟶ 2,546:
6000000 -> 9 wk, 6 d, 10 hr, 40 min
</pre>
 
=={{header|langur}}==
<syntaxhighlight lang="langur">val d = fn(var sec) {
[
fw/wk d hr min sec/,
for[=[]] dm in [7 * 24 * 60 * 60, 24 * 60 * 60, 60 * 60, 60] {
_for ~= [sec \ dm]
sec rem= dm
} ~ [sec],
]
}
 
for seconds in [7259, 86400, 6000000] {
val dur = d(seconds)
write "{{seconds:7}} sec = "
writeln join(", ", for[=[]] k of dur[1] {
if dur[2][k] != 0: _for ~= ["{{dur[2][k]}} {{dur[1][k]}}"]
})
}
</syntaxhighlight>
 
{{out}}
<pre> 7259 sec = 2 hr, 59 sec
86400 sec = 1 d
6000000 sec = 9 wk, 6 d, 10 hr, 40 min
</pre>
 
You could also convert nanoseconds to a langur duration. Langur durations use a separate count for years, months, days, hours, minutes, seconds, and nanoseconds. A "month" and a "year" are difficult to define in terms of seconds, which is one of the reasons langur has a duration type.
 
=={{header|Liberty BASIC}}==
Line 4,128 ⟶ 4,153:
 
0 OK, 0:94</pre>
 
=={{header|Uiua}}==
{{Works with|Uiua|0.12.0-dev.1}}
[https://www.uiua.org/pad?src=0_12_0-dev_1__IyBCdWlsZHMgYSBzdHJpbmcgcmVwcmVzZW50YXRpb24gb2YgYSB0aW1lIHBlcmlvZAoKVW5pdHMg4oaQIHsic2Vjb25kIiAibWludXRlIiAiaG91ciIgImRheSIgIndlZWsifQpEdXJhdGlvbnMg4oaQIFs2MCA2MCAyNCA3IDk5OTldCgojIG51bWJlci1vZi1zZWNvbmRzIC0-IFtTIE0gSCBEIFddClBhcnRzIOKGkCDih4wo4peM4oinKOKKmeKKguKKgyjijIrDt3zil78pKSBEdXJhdGlvbnPiiplbXSkKIyBTdHJpbmdpZnkgYSB1bml0OiB7IlgiICJ1bml0In0gLT4gYm94IlggdW5pdHMiICAgICAgIApTdW5pdCDihpAgL-KNmijiioLiioIpIiAi4o2l4o2cKOKKoTF84o2a4oqC4oqZQHMp4omg4pahIjEi4oqiLgojIFBhcnNlIHNlY29uZHMgYXMgYSBzdHJpbmcgcGVyaW9kOiBOIC0-ICJ4eHgiICAgIApTdHJpbmcg4oaQIMKw4pah4oqiL-KNmiQiXywgXyLih4ziiaFTdW5pdOKWveKKgyjCsXzijYnih4ziip9Vbml0c8Kw4ouVKSBQYXJ0cwriiLUoW-KWoVN0cmluZ10pWzcyNTkgODY0MDAgNjAwMDAwMCAxNDAwMDAwMCAzMTQ0OTU5OSAzMTQ0OTYwMV0K Run it in Uiua Pad!]
<syntaxhighlight lang="uiua">
# Builds a string representation of a time period
 
Units ← {"second" "minute" "hour" "day" "week"}
Durations ← [60 60 24 7 9999]
 
# number-of-seconds -> [S M H D W]
Parts ← ⇌(◌∧(⊙⊂⊃(⌊÷|◿)) Durations⊙[])
# Stringify a unit: {"X" "unit"} -> box"X units"
Sunit ← /⍚(⊂⊂)" "⍥⍜(⊡1|⍚⊂⊙@s)≠□"1"⊢.
# Parse seconds as a string period: N -> "xxx"
String ← °□⊢/⍚$"_, _"⇌≡Sunit▽⊃(±|⍉⇌⊟Units°⋕) Parts
∵([□String])[7259 86400 6000000 14000000 31449599 31449601]
</syntaxhighlight>
{{out}}
<pre>
╭─
╷ ⌜2 hours, 59 seconds⌟
⌜1 day⌟
⌜9 weeks, 6 days, 10 hours, 40 minutes⌟
⌜23 weeks, 1 day, 53 minutes, 20 seconds⌟
⌜51 weeks, 6 days, 23 hours, 59 minutes, 59 seconds⌟
⌜52 weeks, 1 second⌟
</pre>
 
=={{header|Vale}}==
Line 4,281 ⟶ 4,335:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">var duration = Fn.new { |s|
if (s < 1) return "0 sec"
var dur = ""
1,007

edits