format everything with hindent
[packages/time.git] / lib / Data / Time / Clock / Internal / NominalDiffTime.hs
1 {-# OPTIONS -fno-warn-unused-imports #-}
2 {-# LANGUAGE Trustworthy #-}
3
4 module Data.Time.Clock.Internal.NominalDiffTime
5 ( NominalDiffTime
6 , secondsToNominalDiffTime
7 , nominalDiffTimeToSeconds
8 , nominalDay
9 ) where
10
11 import Control.DeepSeq
12 import Data.Data
13 import Data.Fixed
14 import Data.Time.Calendar.Days
15 import Data.Typeable
16
17 -- | This is a length of time, as measured by UTC.
18 -- It has a precision of 10^-12 s.
19 --
20 -- Conversion functions will treat it as seconds.
21 -- For example, @(0.010 :: NominalDiffTime)@ corresponds to 10 milliseconds.
22 --
23 -- It ignores leap-seconds, so it's not necessarily a fixed amount of clock time.
24 -- For instance, 23:00 UTC + 2 hours of NominalDiffTime = 01:00 UTC (+ 1 day),
25 -- regardless of whether a leap-second intervened.
26 newtype NominalDiffTime =
27 MkNominalDiffTime Pico
28 deriving (Eq, Ord, Data, Typeable)
29
30 -- | Create a 'NominalDiffTime' from a number of seconds.
31 --
32 -- @since 1.9.1
33 secondsToNominalDiffTime :: Pico -> NominalDiffTime
34 secondsToNominalDiffTime = MkNominalDiffTime
35
36 -- | Get the seconds in a 'NominalDiffTime'.
37 --
38 -- @since 1.9.1
39 nominalDiffTimeToSeconds :: NominalDiffTime -> Pico
40 nominalDiffTimeToSeconds (MkNominalDiffTime t) = t
41
42 -- necessary because H98 doesn't have "cunning newtype" derivation
43 instance NFData NominalDiffTime -- FIXME: Data.Fixed had no NFData instances yet at time of writing
44 where
45 rnf ndt = seq ndt ()
46
47 instance Enum NominalDiffTime where
48 succ (MkNominalDiffTime a) = MkNominalDiffTime (succ a)
49 pred (MkNominalDiffTime a) = MkNominalDiffTime (pred a)
50 toEnum = MkNominalDiffTime . toEnum
51 fromEnum (MkNominalDiffTime a) = fromEnum a
52 enumFrom (MkNominalDiffTime a) = fmap MkNominalDiffTime (enumFrom a)
53 enumFromThen (MkNominalDiffTime a) (MkNominalDiffTime b) = fmap MkNominalDiffTime (enumFromThen a b)
54 enumFromTo (MkNominalDiffTime a) (MkNominalDiffTime b) = fmap MkNominalDiffTime (enumFromTo a b)
55 enumFromThenTo (MkNominalDiffTime a) (MkNominalDiffTime b) (MkNominalDiffTime c) =
56 fmap MkNominalDiffTime (enumFromThenTo a b c)
57
58 instance Show NominalDiffTime where
59 show (MkNominalDiffTime t) = (showFixed True t) ++ "s"
60
61 -- necessary because H98 doesn't have "cunning newtype" derivation
62 instance Num NominalDiffTime where
63 (MkNominalDiffTime a) + (MkNominalDiffTime b) = MkNominalDiffTime (a + b)
64 (MkNominalDiffTime a) - (MkNominalDiffTime b) = MkNominalDiffTime (a - b)
65 (MkNominalDiffTime a) * (MkNominalDiffTime b) = MkNominalDiffTime (a * b)
66 negate (MkNominalDiffTime a) = MkNominalDiffTime (negate a)
67 abs (MkNominalDiffTime a) = MkNominalDiffTime (abs a)
68 signum (MkNominalDiffTime a) = MkNominalDiffTime (signum a)
69 fromInteger i = MkNominalDiffTime (fromInteger i)
70
71 -- necessary because H98 doesn't have "cunning newtype" derivation
72 instance Real NominalDiffTime where
73 toRational (MkNominalDiffTime a) = toRational a
74
75 -- necessary because H98 doesn't have "cunning newtype" derivation
76 instance Fractional NominalDiffTime where
77 (MkNominalDiffTime a) / (MkNominalDiffTime b) = MkNominalDiffTime (a / b)
78 recip (MkNominalDiffTime a) = MkNominalDiffTime (recip a)
79 fromRational r = MkNominalDiffTime (fromRational r)
80
81 -- necessary because H98 doesn't have "cunning newtype" derivation
82 instance RealFrac NominalDiffTime where
83 properFraction (MkNominalDiffTime a) = (i, MkNominalDiffTime f)
84 where
85 (i, f) = properFraction a
86 truncate (MkNominalDiffTime a) = truncate a
87 round (MkNominalDiffTime a) = round a
88 ceiling (MkNominalDiffTime a) = ceiling a
89 floor (MkNominalDiffTime a) = floor a
90
91 {-# RULES
92 "realToFrac/DiffTime->NominalDiffTime" realToFrac =
93 \ dt -> MkNominalDiffTime (realToFrac dt)
94 "realToFrac/NominalDiffTime->DiffTime" realToFrac =
95 \ (MkNominalDiffTime ps) -> realToFrac ps
96 "realToFrac/NominalDiffTime->Pico" realToFrac =
97 \ (MkNominalDiffTime ps) -> ps
98 "realToFrac/Pico->NominalDiffTime" realToFrac = MkNominalDiffTime
99 #-}
100
101 -- | One day in 'NominalDiffTime'.
102 nominalDay :: NominalDiffTime
103 nominalDay = 86400