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