Convert seconds to compound duration: Difference between revisions

Content added Content deleted
(Add APL)
(Added solution for Action!)
Line 86: Line 86:
print(duration(86400))
print(duration(86400))
print(duration(6000000))</lang>
print(duration(6000000))</lang>

=={{header|Action!}}==
{{libheader|Action! Tool Kit}}
{{libheader|Action! Real Math}}
<lang Action!>INCLUDE "H6:REALMATH.ACT"

TYPE Time=[BYTE w,d,h,m,s]

REAL ms,hs,ds,ws

BYTE FUNC GetCount(REAL POINTER seconds,interval)
REAL tmp
BYTE res

RealDiv(seconds,interval,tmp)
res=Floor(tmp)
RETURN (res)

PROC Decrease(REAL POINTER seconds,interval BYTE count)
REAL tmp

IntToReal(count,tmp)
RealMult(tmp,interval,tmp)
RealSub(seconds,tmp,seconds)
RETURN

PROC Convert(REAL POINTER seconds Time POINTER t)
REAL tmp

t.w=GetCount(seconds,ws)
Decrease(seconds,ws,t.w)
t.d=GetCount(seconds,ds)
Decrease(seconds,ds,t.d)
t.h=GetCount(seconds,hs)
Decrease(seconds,hs,t.h)
t.m=GetCount(seconds,ms)
Decrease(seconds,ms,t.m)
t.s=RealToInt(seconds)
RETURN

BYTE FUNC PrintPart(BYTE first,n CHAR ARRAY u)
IF n=0 THEN
RETURN (first)
FI

IF first=0 THEN
Print(", ")
FI
PrintF("%B %S",n,u)
RETURN (0)

PROC PrintTime(Time POINTER t)
BYTE first

first=1
first=PrintPart(first,t.w,"wk")
first=PrintPart(first,t.d,"d")
first=PrintPart(first,t.h,"hr")
first=PrintPart(first,t.m,"min")
first=PrintPart(first,t.s,"sec")
IF first THEN
Print("0 sec")
FI
RETURN

PROC Test(CHAR ARRAY s)
REAL seconds
Time t

ValR(s,seconds)
PrintR(seconds) Print(" -> ")
Convert(seconds,t)
PrintTime(t) PutE()
RETURN

PROC Main()
REAL r60,r24,r7

Put(125) PutE() ;clear the screen

MathInit()
IntToReal(60,r60)
IntToReal(24,r24)
IntToReal(7,r7)
RealAssign(r60,ms)
RealMult(ms,r60,hs)
RealMult(hs,r24,ds)
RealMult(ds,r7,ws)

Test("7259")
Test("86400")
Test("6000000")
RETURN</lang>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Convert_seconds_to_compound_duration.png Screenshot from Atari 8-bit computer]
<pre>
259 -> 2 hr, 59 sec
86400 -> 1 d
6000000 -> 9 wk, 6 d, 10 hr, 40 min
</pre>


=={{header|Ada}}==
=={{header|Ada}}==