Convert seconds to compound duration: Difference between revisions

 
(19 intermediate revisions by 9 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,519 ⟶ 1,501:
9 wk, 6 d, 10 hr, 40 min
</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls,DateUtils}}
 
 
<syntaxhighlight lang="Delphi">
const TestData: array [0..2] of integer = (7259,86400,6000000);
 
function SecondsToFormatDate(Sec: integer): string;
var DT: TDateTime;
var Weeks: integer;
var AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond: Word;
const SecPerDay = 60 * 60 * 24;
begin
{Convert seconds to Delphi TDateTime}
{which is floating point days}
DT:=Sec / SecPerDay;
{Get weeks and subtract them off}
Weeks:=Trunc(DT/7);
DT:=DT - Weeks * 7;
{Decode date}
DecodeDateTime(DT,AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond);
{Compensate because TDateTime starts on Dec 30th 1899}
if aDay<30 then aDay:=aDay + 1 else aDay:=aDay - 30;
Result:='';
if Weeks<>0 then Result:=Result+IntToStr(Weeks)+' wk, ';
if ADay<>0 then Result:=Result+IntToStr(ADay)+' d, ';
if AHour<>0 then Result:=Result+IntToStr(AHour)+' hr, ';
if AMinute<>0 then Result:=Result+IntToStr(AMinute)+' min, ';
if ASecond<>0 then Result:=Result+IntToStr(ASecond)+' sec, ';
end;
 
procedure ShowFormatedDataTime(Memo: TMemo);
var I: integer;
begin
for I:=0 to High(TestData) do
Memo.Lines.Add(IntToStr(TestData[I])+' = '+SecondsToFormatDate(TestData[I]));
end;
 
 
</syntaxhighlight>
{{out}}
<pre>
7259 = 2 hr, 59 sec,
86400 = 1 d,
6000000 = 9 wk, 6 d, 10 hr, 40 min,
Elapsed Time: 2.900 ms.
 
</pre>
 
 
=={{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,142 ⟶ 2,172:
fmtsecs 6000000
9 wk, 6 d, 10 hr, 40 min</syntaxhighlight>
 
=={{header|Jakt}}==
<syntaxhighlight lang="jakt">
fn main() {
for seconds in [
7259
86400
6000000
] {
println("{}", time_string(seconds))
}
}
 
fn time_string(mut seconds: i64) throws -> String {
mut result = ""
 
mut minutes = seconds / 60
seconds %= 60
if seconds > 0 {
result = format("{} sec", seconds, result)
}
 
mut hours = minutes / 60
minutes %= 60
if minutes > 0 {
result = format(match result {
"" => "{} min"
else => "{} min, {}"
}, minutes, result)
}
 
mut days = hours / 24
hours %= 24
if hours > 0 {
result = format(match result {
"" => "{} hr"
else => "{} hr, {}"
}, hours, result)
}
 
mut weeks = days / 7
days %= 7
if days > 0 {
result = format(match result {
"" => "{} d"
else => "{} d, {}"
}, days, result)
}
if weeks > 0 {
result = format(match result {
"" => "{} wk"
else => "{} wk, {}"
}, weeks, result)
}
 
return result
}
</syntaxhighlight>
 
=={{header|Java}}==
Line 2,346 ⟶ 2,434:
=={{header|jq}}==
{{works with|jq|1.4}}
'''Also works with gojq, the Go implementation'''
<syntaxhighlight lang="jq">def seconds_to_time_string:
def nonzero(text): floor | if . > 0 then "\(.) \(text)" else empty end;
Line 2,390 ⟶ 2,479:
duration(6000000) = "9w, 6d, 10h, 40m"
</pre>
 
=={{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:
 
<syntaxhighlight lang=K>F 100
"1 min, 40 sec"
F 7259
"2 hr, 59 sec"
F 86400
"1 d"
F 6000000
"9 wk, 6 d, 10 hr, 40 min"</syntaxhighlight>
 
tested in ngn/k
 
=={{header|Kotlin}}==
Line 2,440 ⟶ 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,663 ⟶ 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 3,992 ⟶ 4,151:
 
0 OK, 0:94</pre>
 
=={{header|Vale}}==
{{works with|Vale|0.2.0}}
<syntaxhighlight lang="vale">
import stdlib.*;
import stdlib.stdin.*;
import stdlib.math.*;
 
exported func main() {
foreach testCase in [#][
7259,
86400,
6000000,
] {
testCase.timeString().println();
}
}
 
func timeString(seconds int) str {
result = "";
 
minutes = seconds / 60;
set seconds = seconds.mod(60);
if seconds > 0 {
set result = seconds.str() + " sec";
}
 
hours = minutes / 60;
set minutes = minutes.mod(60);
if minutes > 0 {
set result = minutes.str() + if result != "" {
" min, " + result
} else {
" min"
};
}
 
days = hours / 24;
set hours = hours.mod(24);
if hours > 0 {
set result = hours.str() + if result != "" {
" hr, " + result
} else {
" hr"
};
}
 
weeks = days / 7;
set days = days.mod(7);
if days > 0 {
set result = days.str() + if result != "" {
" d, " + result
} else {
" d"
};
}
if weeks > 0 {
set result = weeks.str() + if result != "" {
" wk, " + result
} else {
" wk"
};
}
 
return result;
}
</syntaxhighlight>
 
=={{header|VBA}}==
Line 4,078 ⟶ 4,304:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">var duration = Fn.new { |s|
if (s < 1) return "0 sec"
var dur = ""
885

edits