Convert seconds to compound duration: Difference between revisions

Content added Content deleted
m (→‎{{header|AppleScript}}: Tabs -> 4spaces)
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 141: Line 141:
6001230 SECONDS = 9 WK, 6 D, 11 HR, 30 SEC
6001230 SECONDS = 9 WK, 6 D, 11 HR, 30 SEC
600000000 SECONDS = 992 WK, 10 HR, 40 MIN</pre>
600000000 SECONDS = 992 WK, 10 HR, 40 MIN</pre>



=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
Line 578: Line 577:
604800 1 wk
604800 1 wk
694861 1 wk, 1 d, 1 hr, 1 min, 1 sec
694861 1 wk, 1 d, 1 hr, 1 min, 1 sec
</pre>

=={{header|Batch File}}==
<lang dos>@echo off
::The Main Thing...
for %%d in (7259 86400 6000000) do call :duration %%d
exit/b 0
::/The Main Thing.

::The Function...
:duration
set output=
set /a "wk=%1/604800,rem=%1%%604800"
if %wk% neq 0 set "output= %wk% wk,"

set /a "d=%rem%/86400,rem=%rem%%%86400"
if %d% neq 0 set "output=%output% %d% d,"

set /a "hr=%rem%/3600,rem=%rem%%%3600"
if %hr% neq 0 set "output=%output% %hr% hr,"

set /a "min=%rem%/60,rem=%rem%%%60"
if %min% neq 0 set "output=%output% %min% min,"

if %rem% neq 0 set "output=%output% %rem% sec,"

if %1 gtr 0 echo %1 sec = %output:~1,-1%
goto :EOF
::/The Function.</lang>
{{Out}}
<pre>
7259 sec = 2 hr, 59 sec
86400 sec = 1 d
6000000 sec = 9 wk, 6 d, 10 hr, 40 min
</pre>
</pre>


Line 706: Line 671:
230 NEXT
230 NEXT
240 PRINT</lang>
240 PRINT</lang>

=={{header|Batch File}}==
<lang dos>@echo off
::The Main Thing...
for %%d in (7259 86400 6000000) do call :duration %%d
exit/b 0
::/The Main Thing.

::The Function...
:duration
set output=
set /a "wk=%1/604800,rem=%1%%604800"
if %wk% neq 0 set "output= %wk% wk,"

set /a "d=%rem%/86400,rem=%rem%%%86400"
if %d% neq 0 set "output=%output% %d% d,"

set /a "hr=%rem%/3600,rem=%rem%%%3600"
if %hr% neq 0 set "output=%output% %hr% hr,"

set /a "min=%rem%/60,rem=%rem%%%60"
if %min% neq 0 set "output=%output% %min% min,"

if %rem% neq 0 set "output=%output% %rem% sec,"

if %1 gtr 0 echo %1 sec = %output:~1,-1%
goto :EOF
::/The Function.</lang>
{{Out}}
<pre>
7259 sec = 2 hr, 59 sec
86400 sec = 1 d
6000000 sec = 9 wk, 6 d, 10 hr, 40 min
</pre>


=={{header|beeswax}}==
=={{header|beeswax}}==
Line 1,731: Line 1,730:
9 wk, 6 d, 10 hr, 40 min
9 wk, 6 d, 10 hr, 40 min
</pre>
</pre>

=={{header|Haskell}}==
=={{header|Haskell}}==
<lang haskell>import Control.Monad (forM_)
<lang haskell>import Control.Monad (forM_)
Line 1,858: Line 1,858:
1 d
1 d
9 wk, 6 d, 10 hr, 40 min</pre>
9 wk, 6 d, 10 hr, 40 min</pre>



=={{header|JavaScript}}==
=={{header|JavaScript}}==
Line 2,404: Line 2,403:
6000000 sec = 9 wk, 6 d, 10 hr, 40 min
6000000 sec = 9 wk, 6 d, 10 hr, 40 min
3380521 sec = 5 wk, 4 d, 3 hr, 2 min, 1 sec</pre>
3380521 sec = 5 wk, 4 d, 3 hr, 2 min, 1 sec</pre>

=={{header|Perl 6}}==

The built-in <code>polymod</code> method (which is a generalization of the <code>divmod</code> function known from other languages), is a perfect match for a task like this:

<lang perl6>sub compound-duration ($seconds) {
($seconds.polymod(60, 60, 24, 7) Z <sec min hr d wk>)
.grep(*[0]).reverse.join(", ")
}

# Demonstration:

for 7259, 86400, 6000000 {
say "{.fmt: '%7d'} sec = {compound-duration $_}";
}</lang>

{{out}}
<pre>
7259 sec = 2 hr, 59 sec
86400 sec = 1 d
6000000 sec = 9 wk, 6 d, 10 hr, 40 min
</pre>


=={{header|Phix}}==
=={{header|Phix}}==
Line 2,994: Line 2,971:
{{out}}
{{out}}
All tests pass... there is no output.
All tests pass... there is no output.

=={{header|Raku}}==
(formerly Perl 6)

The built-in <code>polymod</code> method (which is a generalization of the <code>divmod</code> function known from other languages), is a perfect match for a task like this:

<lang perl6>sub compound-duration ($seconds) {
($seconds.polymod(60, 60, 24, 7) Z <sec min hr d wk>)
.grep(*[0]).reverse.join(", ")
}

# Demonstration:

for 7259, 86400, 6000000 {
say "{.fmt: '%7d'} sec = {compound-duration $_}";
}</lang>

{{out}}
<pre>
7259 sec = 2 hr, 59 sec
86400 sec = 1 d
6000000 sec = 9 wk, 6 d, 10 hr, 40 min
</pre>


=={{header|REXX}}==
=={{header|REXX}}==
Line 3,428: Line 3,428:
1 d
1 d
9 wk, 6 d, 10 hr, 40 min</pre>
9 wk, 6 d, 10 hr, 40 min</pre>

=={{header|VBScript}}==
=={{header|VBScript}}==
<lang vb>
<lang vb>