2{-# LANGUAGE ScopedTypeVariables #-}
4{-# OPTIONS_GHC -Wno-identities #-}
10#include "Streamly/Internal/Data/Time/config-clock.h"
13-- Module : Streamly.Internal.Data.Time.TimeSpec
14-- Copyright : (c) 2019 Composewell Technologies
16-- Maintainer : streamly@composewell.com
17-- Stability : experimental
20module Streamly.Internal.Data.Time.TimeSpec
26import Data.Int (Int64)
27import Foreign.Storable (Storable(..), peek)
30import Foreign.C (CTime(..), CLong(..))
33-------------------------------------------------------------------------------
35-------------------------------------------------------------------------------
37{-# INLINE tenPower9 #-}
41-------------------------------------------------------------------------------
42-- TimeSpec representation
43-------------------------------------------------------------------------------
45-- A structure storing seconds and nanoseconds as 'Int64' is the simplest and
46-- fastest way to store practically large quantities of time with efficient
47-- arithmetic operations. If we store nanoseconds using 'Integer' it can store
48-- practically unbounded quantities but it may not be as efficient to
49-- manipulate in performance critical applications. XXX need to measure the
52-- | Data type to represent practically large quantities of time efficiently.
53-- It can represent time up to ~292 billion years at nanosecond resolution.
54data TimeSpec = TimeSpec
55 { sec :: {-# UNPACK #-} !Int64 -- ^ seconds
56 , nsec :: {-# UNPACK #-} !Int64 -- ^ nanoseconds
57 } deriving (Eq, Read, Show)
59-- We assume that nsec is always less than 10^9. When TimeSpec is negative then
60-- both sec and nsec are negative.
61instance Ord TimeSpec where
62 compare (TimeSpec s1 ns1) (TimeSpec s2 ns2) =
67-- make sure nsec is less than 10^9
68{-# INLINE addWithOverflow #-}
69addWithOverflow :: TimeSpec -> TimeSpec -> TimeSpec
70addWithOverflow (TimeSpec s1 ns1) (TimeSpec s2 ns2) =
72 (s', ns) = if nsum > tenPower9 || nsum < negate tenPower9
73 then nsum `divMod` tenPower9
75 in TimeSpec (s1 + s2 + s') ns
77-- make sure both sec and nsec have the same sign
78{-# INLINE adjustSign #-}
79adjustSign :: TimeSpec -> TimeSpec
80adjustSign t@(TimeSpec s ns)
81 | s > 0 && ns < 0 = TimeSpec (s - 1) (ns + tenPower9)
82 | s < 0 && ns > 0 = TimeSpec (s + 1) (ns - tenPower9)
85{-# INLINE timeSpecToInteger #-}
86timeSpecToInteger :: TimeSpec -> Integer
87timeSpecToInteger (TimeSpec s ns) = toInteger $ s * tenPower9 + ns
89instance Num TimeSpec where
91 t1 + t2 = adjustSign (addWithOverflow t1 t2)
93 -- XXX will this be more optimal if imlemented without "negate"?
95 t1 - t2 = t1 + negate t2
96 t1 * t2 = fromInteger $ timeSpecToInteger t1 * timeSpecToInteger t2
99 negate (TimeSpec s ns) = TimeSpec (negate s) (negate ns)
101 abs (TimeSpec s ns) = TimeSpec (abs s) (abs ns)
102 {-# INLINE signum #-}
103 signum (TimeSpec s ns) | s == 0 = TimeSpec (signum ns) 0
104 | otherwise = TimeSpec (signum s) 0
105 -- This is fromNanoSecond64 Integer
106 {-# INLINE fromInteger #-}
107 fromInteger nanosec = TimeSpec (fromInteger s) (fromInteger ns)
108 where (s, ns) = nanosec `divMod` toInteger tenPower9
115instance Storable TimeSpec where
119 CTime s <- peekByteOff p 0
120 CLong ns <- peekByteOff p 4
121 return (TimeSpec (fromIntegral s) (fromIntegral ns))
122 poke p (TimeSpec s ns) = do
123 pokeByteOff p 0 ((fromIntegral s) :: CTime)
124 pokeByteOff p 4 ((fromIntegral ns) :: CLong)
126#elif HS_CLOCK_WINDOWS
127instance Storable TimeSpec where
128 sizeOf _ = sizeOf (undefined :: Int64) * 2
129 alignment _ = alignment (undefined :: Int64)
131 s <- peekByteOff ptr 0
132 ns <- peekByteOff ptr (sizeOf (undefined :: Int64))
133 return (TimeSpec s ns)
135 pokeByteOff ptr 0 (sec ts)
136 pokeByteOff ptr (sizeOf (undefined :: Int64)) (nsec ts)
138instance Storable TimeSpec where
139 sizeOf _ = #{size struct timespec}
140 alignment _ = #{alignment struct timespec}
142 s :: #{type time_t} <- #{peek struct timespec, tv_sec} ptr
143 ns :: #{type long} <- #{peek struct timespec, tv_nsec} ptr
144 return $ TimeSpec (fromIntegral s) (fromIntegral ns)
146 let s :: #{type time_t} = fromIntegral $ sec ts
147 ns :: #{type long} = fromIntegral $ nsec ts
148 #{poke struct timespec, tv_sec} ptr (s)
149 #{poke struct timespec, tv_nsec} ptr (ns)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list