]> gitweb @ CieloNegro.org - time-w3c.git/blob - Data/Time/W3CDateTime.hs
data W3CDateTime
[time-w3c.git] / Data / Time / W3CDateTime.hs
1 module Data.Time.W3CDateTime
2     ( W3CDateTime
3     )
4     where
5
6 import Data.Convertible
7 import Data.Fixed
8 import Data.Time
9 import Data.Typeable
10
11
12 -- This data type is /partially ordered/ so we can't make it an
13 -- instance of Ord (e.g. "2010" and "2010-01" can't be compared).
14 data W3CDateTime
15     = W3CDateTime {
16         w3cYear     :: !Integer
17       , w3cMonth    :: !(Maybe Int)
18       , w3cDay      :: !(Maybe Int)
19       , w3cHour     :: !(Maybe Int)
20       , w3cMinute   :: !(Maybe Int)
21       , w3cSecond   :: !(Maybe Pico)
22       , w3cTimeZone :: !(Maybe TimeZone)
23       }
24     deriving (Show, Eq, Typeable)
25
26 empty :: W3CDateTime
27 empty = W3CDateTime {
28           w3cYear     = 0
29         , w3cMonth    = Nothing
30         , w3cDay      = Nothing
31         , w3cHour     = Nothing
32         , w3cMinute   = Nothing
33         , w3cSecond   = Nothing
34         , w3cTimeZone = Nothing
35         }
36
37 instance Convertible Day W3CDateTime where
38     safeConvert day
39         = case toGregorian day of
40             (y, m, d) -> return empty {
41                            w3cYear  = y
42                          , w3cMonth = Just m
43                          , w3cDay   = Just d
44                          }
45
46 instance Convertible W3CDateTime Day where
47     safeConvert w3c
48         = do let y = w3cYear w3c
49              m <- case w3cMonth w3c of
50                     Just m  -> return m
51                     Nothing -> convError "No month info" w3c
52              d <- case w3cDay w3c of
53                     Just d  -> return d
54                     Nothing -> convError "No day info" w3c
55              return $ fromGregorian y m d