Convert seconds to compound duration: Difference between revisions
Content added Content deleted
(→{{header|zkl}}: another solution) |
|||
Line 65: | Line 65: | ||
__TOC__ |
__TOC__ |
||
=={{header|AWK}}== |
|||
<lang Ada>with Ada.Text_IO; |
|||
procedure Convert is |
|||
type Time is range 0 .. 10_000*356*20*60*60; -- at most 10_000 years |
|||
subtype Valid_Duration is Time range 1 .. 10_000*356*20*60*60; |
|||
type Units is (WK, D, HR, MIN, SEC); |
|||
package IO renames Ada.Text_IO; |
|||
Divide_By: constant array(Units) of Time := (1_000*53, 7, 24, 60, 60); |
|||
Split: array(Units) of Time; |
|||
No_Comma: Units; |
|||
X: Time; |
|||
Test_Cases: array(Positive range <>) of Valid_Duration := |
|||
(6, 60, 3659, 7_259, 86_400, 6_000_000, 6_001_200, 6_001_230, 600_000_000); |
|||
begin |
|||
for Test_Case of Test_Cases loop |
|||
IO.Put(Time'Image(Test_Case) & " SECONDS ="); |
|||
X := Test_Case; |
|||
-- split X up into weeks, days, ..., seconds |
|||
No_Comma := Units'First; |
|||
for Unit in reverse Units loop -- Unit = SEC, ..., WK (in that order) |
|||
Split(Unit) := X mod Divide_By(Unit); |
|||
X := X / Divide_By(Unit); |
|||
if Unit > No_Comma and Split(Unit)>0 then |
|||
No_Comma := Unit; |
|||
end if; |
|||
end loop; |
|||
-- ouput weeks, days, ..., seconds |
|||
for Unit in Units loop -- Unit = WK, .., SEC (in that order) |
|||
if Split(Unit) > 0 then |
|||
IO.Put(Time'Image(Split(Unit)) & " " & Units'Image(Unit) & |
|||
(if No_Comma > Unit then "," else "")); |
|||
end if; |
|||
end loop; |
|||
IO.New_Line; |
|||
end loop; |
|||
end Convert;</lang> |
|||
{{out}} |
|||
<pre> 6 SECONDS = 6 SEC |
|||
60 SECONDS = 1 MIN |
|||
3659 SECONDS = 1 HR, 59 SEC |
|||
7259 SECONDS = 2 HR, 59 SEC |
|||
86400 SECONDS = 1 D |
|||
6000000 SECONDS = 9 WK, 6 D, 10 HR, 40 MIN |
|||
6001200 SECONDS = 9 WK, 6 D, 11 HR |
|||
6001230 SECONDS = 9 WK, 6 D, 11 HR, 30 SEC |
|||
600000000 SECONDS = 992 WK, 10 HR, 40 MIN</pre> |
|||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
Line 116: | Line 177: | ||
694861 1 wk, 1 d, 1 hr, 1 min, 1 sec |
694861 1 wk, 1 d, 1 hr, 1 min, 1 sec |
||
</pre> |
</pre> |
||
=={{header|Batch File}}== |
=={{header|Batch File}}== |
||
<lang dos>@echo off |
<lang dos>@echo off |