51a84abaed2c4c86583b9f8a3e7bc7ed74d43d21
[packages/time.git] / lib / Data / Time / Clock / Internal / DiffTime.hs
1 {-# LANGUAGE Trustworthy #-}
2 {-# OPTIONS -fno-warn-unused-imports #-}
3 module Data.Time.Clock.Internal.DiffTime
4 (
5 -- * Absolute intervals
6 DiffTime,
7 secondsToDiffTime,
8 picosecondsToDiffTime,
9 diffTimeToPicoseconds,
10 ) where
11
12 import Data.Typeable
13 import Data.Data
14 import Data.Ratio ((%))
15 import Data.Fixed
16 import Control.DeepSeq
17
18
19 -- | This is a length of time, as measured by a clock.
20 -- Conversion functions will treat it as seconds.
21 -- It has a precision of 10^-12 s.
22 newtype DiffTime = MkDiffTime Pico deriving (Eq,Ord,Data, Typeable)
23
24 -- necessary because H98 doesn't have "cunning newtype" derivation
25 instance NFData DiffTime where -- FIXME: Data.Fixed had no NFData instances yet at time of writing
26 rnf dt = seq dt ()
27
28 -- necessary because H98 doesn't have "cunning newtype" derivation
29 instance Enum DiffTime where
30 succ (MkDiffTime a) = MkDiffTime (succ a)
31 pred (MkDiffTime a) = MkDiffTime (pred a)
32 toEnum = MkDiffTime . toEnum
33 fromEnum (MkDiffTime a) = fromEnum a
34 enumFrom (MkDiffTime a) = fmap MkDiffTime (enumFrom a)
35 enumFromThen (MkDiffTime a) (MkDiffTime b) = fmap MkDiffTime (enumFromThen a b)
36 enumFromTo (MkDiffTime a) (MkDiffTime b) = fmap MkDiffTime (enumFromTo a b)
37 enumFromThenTo (MkDiffTime a) (MkDiffTime b) (MkDiffTime c) = fmap MkDiffTime (enumFromThenTo a b c)
38
39 instance Show DiffTime where
40 show (MkDiffTime t) = (showFixed True t) ++ "s"
41
42 -- necessary because H98 doesn't have "cunning newtype" derivation
43 instance Num DiffTime where
44 (MkDiffTime a) + (MkDiffTime b) = MkDiffTime (a + b)
45 (MkDiffTime a) - (MkDiffTime b) = MkDiffTime (a - b)
46 (MkDiffTime a) * (MkDiffTime b) = MkDiffTime (a * b)
47 negate (MkDiffTime a) = MkDiffTime (negate a)
48 abs (MkDiffTime a) = MkDiffTime (abs a)
49 signum (MkDiffTime a) = MkDiffTime (signum a)
50 fromInteger i = MkDiffTime (fromInteger i)
51
52 -- necessary because H98 doesn't have "cunning newtype" derivation
53 instance Real DiffTime where
54 toRational (MkDiffTime a) = toRational a
55
56 -- necessary because H98 doesn't have "cunning newtype" derivation
57 instance Fractional DiffTime where
58 (MkDiffTime a) / (MkDiffTime b) = MkDiffTime (a / b)
59 recip (MkDiffTime a) = MkDiffTime (recip a)
60 fromRational r = MkDiffTime (fromRational r)
61
62 -- necessary because H98 doesn't have "cunning newtype" derivation
63 instance RealFrac DiffTime where
64 properFraction (MkDiffTime a) = let (b',a') = properFraction a in (b',MkDiffTime a')
65 truncate (MkDiffTime a) = truncate a
66 round (MkDiffTime a) = round a
67 ceiling (MkDiffTime a) = ceiling a
68 floor (MkDiffTime a) = floor a
69
70 -- | Create a 'DiffTime' which represents an integral number of seconds.
71 secondsToDiffTime :: Integer -> DiffTime
72 secondsToDiffTime = fromInteger
73
74 -- | Create a 'DiffTime' from a number of picoseconds.
75 picosecondsToDiffTime :: Integer -> DiffTime
76 picosecondsToDiffTime x = MkDiffTime (MkFixed x)
77
78 -- | Get the number of picoseconds in a 'DiffTime'.
79 diffTimeToPicoseconds :: DiffTime -> Integer
80 diffTimeToPicoseconds (MkDiffTime (MkFixed x)) = x
81
82 {-# RULES
83 "realToFrac/DiffTime->Pico" realToFrac = \ (MkDiffTime ps) -> ps
84 "realToFrac/Pico->DiffTime" realToFrac = MkDiffTime
85 #-}