Commit 3f52e4ad authored by Mart Lubbers's avatar Mart Lubbers

Add arithmetic operations to timespec

parent 77b02c8b
Pipeline #8855 passed with stage
in 1 minute and 26 seconds
......@@ -90,3 +90,10 @@ toGmTime :: !Timestamp -> Tm
*/
nsTime :: !*World -> (!Timespec, !*World)
:: Timespec = {tv_sec :: !Int, tv_nsec :: !Int}
timespecToStamp :: !Timespec -> Timestamp
timestampToSpec :: !Timestamp -> Timespec
instance < Timespec
instance + Timespec
instance - Timespec
......@@ -176,3 +176,30 @@ nsTime w
# (tv_sec, p) = readIntP p 0
# (tv_nsec, p) = readIntP p 8
= ({Timespec | tv_sec = tv_sec, tv_nsec = tv_nsec}, freeSt p w)
timespecToStamp :: !Timespec -> Timestamp
timespecToStamp t = Timestamp t.tv_sec
timestampToSpec :: !Timestamp -> Timespec
timestampToSpec (Timestamp t) = {tv_sec=t,tv_nsec=0}
instance < Timespec
where
(<) t1 t2
| t1.tv_sec == t2.tv_sec = t1.tv_nsec < t2.tv_nsec
= t1.tv_sec < t2.tv_sec
instance + Timespec
where
(+) t1 t2 = let tv_nsec = t1.tv_nsec + t2.tv_nsec in
{ tv_sec = t1.tv_sec + t2.tv_sec + tv_nsec / 1000000000
, tv_nsec = tv_nsec rem 1000000000
}
instance - Timespec
where
(-) t1 t2
# tv_nsec = t1.tv_nsec - t2.tv_nsec
| tv_nsec < 0
= {tv_sec = t1.tv_sec - t2.tv_sec - 1, tv_nsec = 1000000000 - tv_nsec}
= {tv_sec = t1.tv_sec - t2.tv_sec - 1, tv_nsec = tv_nsec}
......@@ -94,3 +94,11 @@ gmTimeC :: !{#Int} -> Int
*/
nsTime :: !*World -> (!Timespec, !*World)
:: Timespec = {tv_sec :: !Int, tv_nsec :: !Int}
timespecToStamp :: !Timespec -> Timestamp
timestampToSpec :: !Timestamp -> Timespec
instance < Timespec
instance + Timespec
instance - Timespec
......@@ -180,3 +180,30 @@ uintToInt :: Int -> Integer
uintToInt i
| i < 0 = toInteger i + {integer_s=0,integer_a={0,1}}
= toInteger i
timespecToStamp :: !Timespec -> Timestamp
timespecToStamp t = Timestamp t.tv_sec
timestampToSpec :: !Timestamp -> Timespec
timestampToSpec (Timestamp t) = {tv_sec=t,tv_nsec=0}
instance < Timespec
where
(<) t1 t2
| t1.tv_sec == t2.tv_sec = t1.tv_nsec < t2.tv_nsec
= t1.tv_sec < t2.tv_sec
instance + Timespec
where
(+) t1 t2 = let tv_nsec = t1.tv_nsec + t2.tv_nsec in
{ tv_sec = t1.tv_sec + t2.tv_sec + tv_nsec / 1000000000
, tv_nsec = tv_nsec rem 1000000000
}
instance - Timespec
where
(-) t1 t2
# tv_nsec = t1.tv_nsec - t2.tv_nsec
| tv_nsec < 0
= {tv_sec = t1.tv_sec - t2.tv_sec - 1, tv_nsec = 1000000000 - tv_nsec}
= {tv_sec = t1.tv_sec - t2.tv_sec - 1, tv_nsec = tv_nsec}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment