Convert seconds to compound duration: Difference between revisions

 
(13 intermediate revisions by 6 users not shown)
Line 385:
 
=={{header|AppleScript}}==
===Functional===
 
<syntaxhighlight lang="applescript">
-------------------- COMPOUND DURATIONS ------------------
Line 545:
 
----
===Straightforward===
 
<syntaxhighlight lang="applescript">on secondsToCompoundDuration(sec)
A more straightforward solution:
if ((sec's class is not integer) or (sec < 0)) then ¬
 
<syntaxhighlight lang="applescript">on secondsToCompoundDuration(s)
if ((s's class is not integer) or (s < 0)) then
error "secondsToCompoundDuration() handler only accepts positive integers."
-- The task description notwithstanding, return "0 sec" if the input is 0.
else if (s = 0) then
if (sec = 0) -- The task description notwithstanding,then return "0 sec" if the input is the positive integer 0.
-- Otherwise perform the described task.
set output to "0 sec"
set units to {weeks, days, hours, minutes, 1}
else
set suffixes to {" wk, ", " d, ", " hr, ", " min, ", " sec, "}
-- Otherwise perform the described task.
set suffixesoutput to {" wk", " d", " hr", " min", " sec"}
-- AppleScript has constants for the number of seconds in a week, a day, an hour, and a minute.
repeat with i from 1 to 5
set durationConstants to {weeks, days, hours, minutes, 1}
--set Initialise a listunit to collectunits's theitem output.i
set outputunitValue to {}sec div unit
if (unitValue > 0) then set output to output & unitValue & suffixes's item i
--set Worksec throughto thesec listmod of constants.unit
repeatif with(sec i= from0) 1then toexit 5repeat
end repeat
-- Get the next constant and divide the number of seconds by it to get the number of corresponding units.
set thisConstant to item i of durationConstants
set unitValue to s div thisConstant
-- If the number of units isn't zero, coerce it to text, concatenate the corresponding suffix, and add the result to the output list.
if (unitValue > 0) then set end of output to (unitValue as text) & item i of suffixes
-- Get the number of seconds left over for use in the next iteration of the repeat.
set s to s mod thisConstant
-- Short-cut out of the repeat if no seconds remaining, although this isn't strictly necessary.
if (s = 0) then exit repeat
end repeat
-- Coerce the list of collected results to a single text using ", " as a delimiter.
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to ", "
set output to output as text
set AppleScript's text item delimiters to astid
end if
return output's text 1 thru -3
end secondsToCompoundDuration
 
Line 590 ⟶ 572:
 
{{output}}
<presyntaxhighlight lang="applescript">"2 hr, 59 sec
1 d
9 wk, 6 d, 10 hr, 40 min"</presyntaxhighlight>
 
=={{header|Applesoft BASIC}}==
Line 1,573 ⟶ 1,555:
=={{header|EasyLang}}==
<syntaxhighlight lang="text">
func$ 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,502 ⟶ 2,481:
 
=={{header|K}}==
{{works with|ngn/k}}<syntaxhighlight lang=K>F:{", "/" "/'+($x[s]),s:,&0<x}(" "\"wk d hr min sec")!0 7 24 60 60\</syntaxhighlight>
 
Examples:
Line 2,566 ⟶ 2,545:
86400 -> 1 d
6000000 -> 9 wk, 6 d, 10 hr, 40 min
</pre>
 
=={{header|langur}}==
<syntaxhighlight lang="langur">val .duration = 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 = .duration(.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>
 
Line 3,789 ⟶ 3,794:
see " seconds" + nl ok
</syntaxhighlight>
 
=={{header|RPL}}==
≪ MOD LAST / FLOOR ≫ '<span style="color:blue">'''DIVMOD'''</span>' STO
≪ {" wk" " d" " hr" " min" " sec" } → unit
≪ 60 <span style="color:blue">'''DIVMOD'''</span> 60 <span style="color:blue">'''DIVMOD'''</span> 24 <span style="color:blue">'''DIVMOD'''</span> 7 <span style="color:blue">'''DIVMOD'''</span>
1 SF ""
1 unit SIZE '''FOR''' j
'''IF''' SWAP '''THEN'''
LAST →STR unit j GET +
'''IF''' 1 FC?C '''THEN''' ", " SWAP + '''END'''
+ '''END'''
'''NEXT'''
≫ ≫ '<span style="color:blue">'''→CDUR'''</span>' STO
Users of HP-48G and newer models can replace the <code>60 <span style="color:blue">'''DIVMOD'''</span> 60 <span style="color:blue">'''DIVMOD'''</span> 24 <span style="color:blue">'''DIVMOD'''</span> 7 <span style="color:blue">'''DIVMOD'''</span></code> line by:
{ 60 60 24 7 } 1 ≪ MOD LAST / FLOOR ≫ DOSUBS OBJ→ DROP
 
7259 <span style="color:blue">'''→CDUR'''</span>
86400 <span style="color:blue">'''→CDUR'''</span>
6000000 <span style="color:blue">'''→CDUR'''</span>
10! <span style="color:blue">'''→CDUR'''</span>
{{out}}
<pre>
4: "2 hr, 59 sec"
3: "1 d"
2: "9 wk, 6 d, 10 hr, 40 min"
1: "6 wk"
</pre>
 
=={{header|Ruby}}==
Line 4,271 ⟶ 4,304:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">var duration = Fn.new { |s|
if (s < 1) return "0 sec"
var dur = ""
885

edits