2{-# LANGUAGE ScopedTypeVariables #-}
4{-# OPTIONS_GHC -Wno-identities #-}
10#include "Streamly/Internal/Data/Time/Clock/config-clock.h"
15-- Module : Streamly.Internal.Data.Time.TimeSpec
16-- Copyright : (c) 2019 Composewell Technologies
18-- Maintainer : streamly@composewell.com
19-- Stability : experimental
22module Streamly.Internal.Data.Time.TimeSpec
28import Data.Int (Int64)
29#if (WORD_SIZE_IN_BITS == 32)
30import Data.Int (Int32)
32import Foreign.Storable (Storable(..), peek)
35import Foreign.C (CTime(..), CLong(..))
38-------------------------------------------------------------------------------
40-------------------------------------------------------------------------------
42{-# INLINE tenPower9 #-}
46-------------------------------------------------------------------------------
47-- TimeSpec representation
48-------------------------------------------------------------------------------
50-- A structure storing seconds and nanoseconds as 'Int64' is the simplest and
51-- fastest way to store practically large quantities of time with efficient
52-- arithmetic operations. If we store nanoseconds using 'Integer' it can store
53-- practically unbounded quantities but it may not be as efficient to
54-- manipulate in performance critical applications. XXX need to measure the
57-- | Data type to represent practically large quantities of time efficiently.
58-- It can represent time up to ~292 billion years at nanosecond resolution.
59data TimeSpec = TimeSpec
60 { sec :: {-# UNPACK #-} !Int64 -- ^ seconds
61 , nsec :: {-# UNPACK #-} !Int64 -- ^ nanoseconds
62 } deriving (Eq, Read, Show)
64-- We assume that nsec is always less than 10^9. When TimeSpec is negative then
65-- both sec and nsec are negative.
66instance Ord TimeSpec where
67 compare (TimeSpec s1 ns1) (TimeSpec s2 ns2) =
72-- make sure nsec is less than 10^9
73{-# INLINE addWithOverflow #-}
74addWithOverflow :: TimeSpec -> TimeSpec -> TimeSpec
75addWithOverflow (TimeSpec s1 ns1) (TimeSpec s2 ns2) =
77 (s', ns) = if nsum > tenPower9 || nsum < negate tenPower9
78 then nsum `divMod` tenPower9
80 in TimeSpec (s1 + s2 + s') ns
82-- make sure both sec and nsec have the same sign
83{-# INLINE adjustSign #-}
84adjustSign :: TimeSpec -> TimeSpec
85adjustSign t@(TimeSpec s ns)
86 | s > 0 && ns < 0 = TimeSpec (s - 1) (ns + tenPower9)
87 | s < 0 && ns > 0 = TimeSpec (s + 1) (ns - tenPower9)
90{-# INLINE timeSpecToInteger #-}
91timeSpecToInteger :: TimeSpec -> Integer
92timeSpecToInteger (TimeSpec s ns) = toInteger $ s * tenPower9 + ns
94instance Num TimeSpec where
96 t1 + t2 = adjustSign (addWithOverflow t1 t2)
98 -- XXX will this be more optimal if imlemented without "negate"?
100 t1 - t2 = t1 + negate t2
101 t1 * t2 = fromInteger $ timeSpecToInteger t1 * timeSpecToInteger t2
103 {-# INLINE negate #-}
104 negate (TimeSpec s ns) = TimeSpec (negate s) (negate ns)
106 abs (TimeSpec s ns) = TimeSpec (abs s) (abs ns)
107 {-# INLINE signum #-}
108 signum (TimeSpec s ns) | s == 0 = TimeSpec (signum ns) 0
109 | otherwise = TimeSpec (signum s) 0
110 -- This is fromNanoSecond64 Integer
111 {-# INLINE fromInteger #-}
112 fromInteger nanosec = TimeSpec (fromInteger s) (fromInteger ns)
113 where (s, ns) = nanosec `divMod` toInteger tenPower9
120instance Storable TimeSpec where
124 CTime s <- peekByteOff p 0
125 CLong ns <- peekByteOff p 4
126 return (TimeSpec (fromIntegral s) (fromIntegral ns))
127 poke p (TimeSpec s ns) = do
128 pokeByteOff p 0 ((fromIntegral s) :: CTime)
129 pokeByteOff p 4 ((fromIntegral ns) :: CLong)
131#elif HS_CLOCK_WINDOWS
132instance Storable TimeSpec where
133 sizeOf _ = sizeOf (undefined :: Int64) * 2
134 alignment _ = alignment (undefined :: Int64)
136 s <- peekByteOff ptr 0
137 ns <- peekByteOff ptr (sizeOf (undefined :: Int64))
138 return (TimeSpec s ns)
140 pokeByteOff ptr 0 (sec ts)
141 pokeByteOff ptr (sizeOf (undefined :: Int64)) (nsec ts)
143instance Storable TimeSpec where
144 sizeOf _ = #{size struct timespec}
145 alignment _ = #{alignment struct timespec}
147 s :: #{type time_t} <- #{peek struct timespec, tv_sec} ptr
148 ns :: #{type long} <- #{peek struct timespec, tv_nsec} ptr
149 return $ TimeSpec (fromIntegral s) (fromIntegral ns)
151 let s :: #{type time_t} = fromIntegral $ sec ts
152 ns :: #{type long} = fromIntegral $ nsec ts
153 #{poke struct timespec, tv_sec} ptr (s)
154 #{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