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