@@ -0,0 +1,156 @@
2+{-# LANGUAGE ScopedTypeVariables #-}
4+#if __GLASGOW_HASKELL__ >= 800
5+{-# OPTIONS_GHC -Wno-identities #-}
12+#include "config-clock.h"
15+-- Module : Streamly.Internal.Data.Time.TimeSpec
16+-- Copyright : (c) 2019 Composewell Technologies
18+-- Maintainer : streamly@composewell.com
19+-- Stability : experimental
22+module Streamly.Internal.Data.Time.TimeSpec
28+import Data.Int (Int64)
29+import Foreign.Storable (Storable(..), peek)
32+import Foreign.C (CTime(..), CLong(..))
35+-------------------------------------------------------------------------------
37+-------------------------------------------------------------------------------
39+{-# INLINE tenPower9 #-}
41+tenPower9 = 1000000000
43+-------------------------------------------------------------------------------
44+-- TimeSpec representation
45+-------------------------------------------------------------------------------
47+-- A structure storing seconds and nanoseconds as 'Int64' is the simplest and
48+-- fastest way to store practically large quantities of time with efficient
49+-- arithmetic operations. If we store nanoseconds using 'Integer' it can store
50+-- practically unbounded quantities but it may not be as efficient to
51+-- manipulate in performance critical applications. XXX need to measure the
54+-- | Data type to represent practically large quantities of time efficiently.
55+-- It can represent time up to ~292 billion years at nanosecond resolution.
56+data TimeSpec = TimeSpec
57+ { sec :: {-# UNPACK #-} !Int64 -- ^ seconds
58+ , nsec :: {-# UNPACK #-} !Int64 -- ^ nanoseconds
59+ } deriving (Eq, Read, Show)
61+-- We assume that nsec is always less than 10^9. When TimeSpec is negative then
62+-- both sec and nsec are negative.
63+instance Ord TimeSpec where
64+ compare (TimeSpec s1 ns1) (TimeSpec s2 ns2) =
69+-- make sure nsec is less than 10^9
70+{-# INLINE addWithOverflow #-}
71+addWithOverflow :: TimeSpec -> TimeSpec -> TimeSpec
72+addWithOverflow (TimeSpec s1 ns1) (TimeSpec s2 ns2) =
74+ (s', ns) = if nsum > tenPower9 || nsum < negate tenPower9
75+ then nsum `divMod` tenPower9
77+ in TimeSpec (s1 + s2 + s') ns
79+-- make sure both sec and nsec have the same sign
80+{-# INLINE adjustSign #-}
81+adjustSign :: TimeSpec -> TimeSpec
82+adjustSign t@(TimeSpec s ns)
83+ | s > 0 && ns < 0 = TimeSpec (s - 1) (ns + tenPower9)
84+ | s < 0 && ns > 0 = TimeSpec (s + 1) (ns - tenPower9)
87+{-# INLINE timeSpecToInteger #-}
88+timeSpecToInteger :: TimeSpec -> Integer
89+timeSpecToInteger (TimeSpec s ns) = toInteger $ s * tenPower9 + ns
91+instance Num TimeSpec where
93+ t1 + t2 = adjustSign (addWithOverflow t1 t2)
95+ -- XXX will this be more optimal if imlemented without "negate"?
97+ t1 - t2 = t1 + negate t2
98+ t1 * t2 = fromInteger $ timeSpecToInteger t1 * timeSpecToInteger t2
100+ {-# INLINE negate #-}
101+ negate (TimeSpec s ns) = TimeSpec (negate s) (negate ns)
103+ abs (TimeSpec s ns) = TimeSpec (abs s) (abs ns)
104+ {-# INLINE signum #-}
105+ signum (TimeSpec s ns) | s == 0 = TimeSpec (signum ns) 0
106+ | otherwise = TimeSpec (signum s) 0
107+ -- This is fromNanoSecond64 Integer
108+ {-# INLINE fromInteger #-}
109+ fromInteger nanosec = TimeSpec (fromInteger s) (fromInteger ns)
110+ where (s, ns) = nanosec `divMod` toInteger tenPower9
116+#if __GLASGOW_HASKELL__ < 800
117+#let alignment t = "%lu", (unsigned long)offsetof(struct {char x__; t (y__); }, y__)
120+#ifdef HS_CLOCK_GHCJS
121+instance Storable TimeSpec where
125+ CTime s <- peekByteOff p 0
126+ CLong ns <- peekByteOff p 4
127+ return (TimeSpec (fromIntegral s) (fromIntegral ns))
128+ poke p (TimeSpec s ns) = do
129+ pokeByteOff p 0 ((fromIntegral s) :: CTime)
130+ pokeByteOff p 4 ((fromIntegral ns) :: CLong)
132+#elif HS_CLOCK_WINDOWS
133+instance Storable TimeSpec where
134+ sizeOf _ = sizeOf (undefined :: Int64) * 2
135+ alignment _ = alignment (undefined :: Int64)
137+ s <- peekByteOff ptr 0
138+ ns <- peekByteOff ptr (sizeOf (undefined :: Int64))
139+ return (TimeSpec s ns)
141+ pokeByteOff ptr 0 (sec ts)
142+ pokeByteOff ptr (sizeOf (undefined :: Int64)) (nsec ts)
144+instance Storable TimeSpec where
145+ sizeOf _ = #{size struct timespec}
146+ alignment _ = #{alignment struct timespec}
148+ s :: #{type time_t} <- #{peek struct timespec, tv_sec} ptr
149+ ns :: #{type long} <- #{peek struct timespec, tv_nsec} ptr
150+ return $ TimeSpec (fromIntegral s) (fromIntegral ns)
152+ let s :: #{type time_t} = fromIntegral $ sec ts
153+ ns :: #{type long} = fromIntegral $ nsec ts
154+ #{poke struct timespec, tv_sec} ptr (s)
155+ #{poke struct timespec, tv_nsec} ptr (ns)