1 -- | Data types defined by this package.
2 module Data.Time.W3C.Types
7 import Data.Convertible
13 -- |'W3CDateTime' represents a W3C Date and Time format.
15 -- The field 'w3cYear' is mandatory while other fields are
16 -- optional. But you should be careful about combinations of such
17 -- optional fields. No combinations are allowed except for the
26 -- * YYYY-MM-DDThh:mmTZD
28 -- * YYYY-MM-DDThh:mm:ss.sTZD
30 -- This data type is /partially ordered/ so we can't make it an
31 -- instance of Ord (e.g. @\"2010\"@ and @\"2010-01\"@ can't be
36 , w3cMonth :: !(Maybe Int)
37 , w3cDay :: !(Maybe Int)
38 , w3cHour :: !(Maybe Int)
39 , w3cMinute :: !(Maybe Int)
40 , w3cSecond :: !(Maybe Pico)
41 , w3cTimeZone :: !(Maybe TimeZone)
43 deriving (Show, Eq, Typeable)
45 fetch :: (Show a, Typeable a, Typeable b) =>
52 Nothing -> convError ("No " ++ name ++ " information in the given value") a
55 instance Convertible W3CDateTime W3CDateTime where
58 instance Convertible Day W3CDateTime where
60 = case toGregorian day of
61 (y, m, d) -> return W3CDateTime {
68 , w3cTimeZone = Nothing
71 instance Convertible W3CDateTime Day where
73 = do let y = w3cYear w3c
74 m <- fetch "month" w3cMonth w3c
75 d <- fetch "day" w3cDay w3c
76 return (fromGregorian y m d)
78 instance Convertible ZonedTime W3CDateTime where
80 = let lt = zonedTimeToLocalTime zt
83 hms = localTimeOfDay lt
86 w3cYear = case toGregorian ymd of (y, _, _) -> y
87 , w3cMonth = Just (case toGregorian ymd of (_, m, _) -> m)
88 , w3cDay = Just (case toGregorian ymd of (_, _, d) -> d)
89 , w3cHour = Just (todHour hms)
90 , w3cMinute = Just (todMin hms)
91 , w3cSecond = Just (todSec hms)
92 , w3cTimeZone = Just tz
95 instance Convertible W3CDateTime ZonedTime where
97 = do day <- safeConvert w3c
98 tod <- do h <- fetch "hour" w3cHour w3c
99 m <- fetch "minute" w3cMinute w3c
100 s <- fetch "second" w3cSecond w3c
101 case makeTimeOfDayValid h m s of
102 Just tod -> return tod
103 Nothing -> convError "Invalid time of day" w3c
104 tz <- fetch "timezone" w3cTimeZone w3c
106 zonedTimeToLocalTime = LocalTime day tod