File: Types.hs

package info (click to toggle)
haskell-hourglass 0.2.12-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 256 kB
  • sloc: haskell: 1,398; ansic: 19; makefile: 3
file content (245 lines) | stat: -rw-r--r-- 7,789 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE DeriveDataTypeable #-}
-- |
-- Module      : Time.Types
-- License     : BSD-style
-- Maintainer  : Vincent Hanquez <vincent@snarc.org>
--
-- Basic times units and types.
--
-- While pratically some units could hold infinite values, for practical
-- and efficient purpose they are limited to int64 types for seconds
-- and int types for years.
--
-- Most units use the unix epoch referential, but by no means reduce portability.
-- the unix referential works under the Windows platform or any other platforms.
--
module Time.Types
    (
    -- * Time units
      NanoSeconds(..)
    , Seconds(..)
    , Minutes(..)
    , Hours(..)
    , TimeInterval(..)
    -- * Time enumeration
    , Month(..)
    , WeekDay(..)
    -- * Timezone
    , TimezoneOffset(..)
    , timezoneOffsetToSeconds
    , timezone_UTC
    -- * Computer friendly format
    -- ** Unix elapsed
    , Elapsed(..)
    , ElapsedP(..)
    -- * Human friendly format
    -- ** Calendar time
    , Date(..)
    , TimeOfDay(..)
    , DateTime(..)
    ) where

import Data.Int
import Data.Data
import Data.Ratio
import Control.DeepSeq
import Data.Hourglass.Utils (pad2)

-- | Represent any time interval that has an
-- equivalent value to a number of seconds.
class TimeInterval i where
    toSeconds   :: i -> Seconds
    fromSeconds :: Seconds -> (i, Seconds)

-- | Nanoseconds
newtype NanoSeconds = NanoSeconds Int64
    deriving (Read,Eq,Ord,Num,Data,Typeable,NFData)

instance Show NanoSeconds where
    show (NanoSeconds v) = shows v "ns"

instance TimeInterval NanoSeconds where
    toSeconds (NanoSeconds ns) = Seconds (ns `div` 1000000000)
    fromSeconds (Seconds s) = (NanoSeconds (s * 1000000000), 0)

-- | Number of seconds without a referential.
--
-- Can hold a number between [-2^63,2^63-1], which should
-- be good for some billions of years.
--
-- However, because of limitation in the calendar conversion
-- currently used, seconds should be in the range [-2^55,2^55-1],
-- which is good for only 1 billion of year.
newtype Seconds = Seconds Int64
    deriving (Read,Eq,Ord,Enum,Num,Real,Integral,Data,Typeable,NFData)

instance Show Seconds where
    show (Seconds s) = shows s "s"

instance TimeInterval Seconds where
    toSeconds   = id
    fromSeconds s = (s,0)

-- | Number of minutes without a referential.
newtype Minutes = Minutes Int64
    deriving (Read,Eq,Ord,Enum,Num,Real,Integral,Data,Typeable,NFData)

instance Show Minutes where
    show (Minutes s) = shows s "m"

instance TimeInterval Minutes where
    toSeconds (Minutes m)   = Seconds (m * 60)
    fromSeconds (Seconds s) = (Minutes m, Seconds s')
      where (m, s') = s `divMod` 60

-- | Number of hours without a referential.
newtype Hours = Hours Int64
    deriving (Read,Eq,Ord,Enum,Num,Real,Integral,Data,Typeable,NFData)

instance Show Hours where
    show (Hours s) = shows s "h"

instance TimeInterval Hours where
    toSeconds (Hours h)     = Seconds (h * 3600)
    fromSeconds (Seconds s) = (Hours h, Seconds s')
      where (h, s') = s `divMod` 3600

-- | A number of seconds elapsed since the unix epoch.
newtype Elapsed = Elapsed Seconds
    deriving (Read,Eq,Ord,Num,Data,Typeable,NFData)

instance Show Elapsed where
    show (Elapsed s) = show s

-- | A number of seconds and nanoseconds elapsed since the unix epoch.
data ElapsedP = ElapsedP {-# UNPACK #-} !Elapsed {-# UNPACK #-} !NanoSeconds
    deriving (Read,Eq,Ord,Data,Typeable)

instance Show ElapsedP where
    show (ElapsedP e ns) = shows e ('.' : show ns)

instance NFData ElapsedP where rnf e = e `seq` ()

instance Num ElapsedP where
    (+) = addElapsedP
    (-) = subElapsedP
    (ElapsedP e1 ns1) * (ElapsedP e2 ns2) = ElapsedP (e1*e2) (ns1*ns2)
    negate (ElapsedP e ns) = ElapsedP (negate e) ns
    abs (ElapsedP e ns)    = ElapsedP (abs e) ns
    signum (ElapsedP e ns) = ElapsedP (signum e) ns
    fromInteger i          = ElapsedP (Elapsed (fromIntegral i)) 0

addElapsedP :: ElapsedP -> ElapsedP -> ElapsedP
addElapsedP (ElapsedP e1 (NanoSeconds ns1)) (ElapsedP e2 (NanoSeconds ns2)) =
    let notNormalizedNS = ns1 + ns2
        (retainedNS, ns) = notNormalizedNS `divMod` 1000000000
    in  ElapsedP (e1 + e2 + (Elapsed $ Seconds retainedNS)) (NanoSeconds ns)

subElapsedP :: ElapsedP -> ElapsedP -> ElapsedP
subElapsedP (ElapsedP e1 (NanoSeconds ns1)) (ElapsedP e2 (NanoSeconds ns2)) =
    let notNormalizedNS = ns1 - ns2
        notNormalizedS  = e1 - e2
    in  if notNormalizedNS < 0
            then ElapsedP (notNormalizedS - oneSecond) (NanoSeconds (1000000000 + notNormalizedNS))
            else ElapsedP notNormalizedS (NanoSeconds notNormalizedNS)
  where
    oneSecond :: Elapsed
    oneSecond = Elapsed $ Seconds 1

instance Real ElapsedP where
    toRational (ElapsedP (Elapsed (Seconds s)) (NanoSeconds 0)) =
        fromIntegral s
    toRational (ElapsedP (Elapsed (Seconds s)) (NanoSeconds ns)) =
        fromIntegral s + (fromIntegral ns % 1000000000)

-- | Month of the year
data Month =
      January
    | February
    | March
    | April
    | May
    | June
    | July
    | August
    | September
    | October
    | November
    | December
    deriving (Show,Read,Eq,Ord,Enum,Data,Typeable,Bounded)

-- | Day of the week
--
-- the enumeration starts on Sunday.
data WeekDay =
      Sunday
    | Monday
    | Tuesday
    | Wednesday
    | Thursday
    | Friday
    | Saturday
    deriving (Show,Read,Eq,Ord,Enum,Data,Typeable,Bounded)

-- | Offset against UTC in minutes to obtain from UTC time, local time.
--
-- * a positive number represent a location East of UTC.
--
-- * a negative number represent a location West of UTC.
--
-- LocalTime t (-300) = t represent a time at UTC-5
-- LocalTime t (+480) = t represent a time at UTC+8
--
-- should be between -11H and +14H
--
-- Example:
--    in AUSEDT (UTC+1000 with daylight = UTC+1100), local time is 15:47;
--    Thus, UTC time is 04:47, and TimezoneOffset is +660 (minutes)
--
newtype TimezoneOffset = TimezoneOffset
    { timezoneOffsetToMinutes :: Int -- ^ return the number of minutes
    } deriving (Eq,Ord,Data,Typeable,NFData)

-- | Return the number of seconds associated with a timezone
timezoneOffsetToSeconds :: TimezoneOffset -> Seconds
timezoneOffsetToSeconds (TimezoneOffset ofs) = Seconds (fromIntegral ofs * 60)

instance Show TimezoneOffset where
    show (TimezoneOffset tz) =
        concat [if tz < 0 then "-" else "+", pad2 tzH, pad2 tzM]
      where (tzH, tzM) = abs tz `divMod` 60

-- | The UTC timezone. offset of 0
timezone_UTC :: TimezoneOffset
timezone_UTC = TimezoneOffset 0

-- | human date representation using common calendar
data Date = Date
    { dateYear  :: {-# UNPACK #-} !Int   -- ^ year (Common Era)
    , dateMonth :: !Month                -- ^ month of the year
    , dateDay   :: {-# UNPACK #-} !Int   -- ^ day of the month, between 1 to 31
    } deriving (Show,Read,Eq,Ord,Data,Typeable)

instance NFData Date where
    rnf (Date y m d) = y `seq` m `seq` d `seq` ()

-- | human time representation of hour, minutes, seconds in a day.
data TimeOfDay = TimeOfDay
    { todHour :: {-# UNPACK #-} !Hours   -- ^ hours, between 0 and 23
    , todMin  :: {-# UNPACK #-} !Minutes -- ^ minutes, between 0 and 59
    , todSec  :: {-# UNPACK #-} !Seconds -- ^ seconds, between 0 and 59. 60 when having leap second */
    , todNSec :: {-# UNPACK #-} !NanoSeconds -- ^ nanoseconds, between 0 and 999999999 */
    } deriving (Show,Read,Eq,Ord,Data,Typeable)

instance NFData TimeOfDay where
    rnf (TimeOfDay h m s ns) = h `seq` m `seq` s `seq` ns `seq` ()

-- | Date and Time
data DateTime = DateTime
    { dtDate :: Date
    , dtTime :: TimeOfDay
    } deriving (Show,Read,Eq,Ord,Data,Typeable)

instance NFData DateTime where
    rnf (DateTime d t) = rnf d `seq` rnf t `seq` ()