1 module Data.Time.W3CDateTime.Types
6 import Data.Convertible
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).
17 , w3cMonth :: !(Maybe Int)
18 , w3cDay :: !(Maybe Int)
19 , w3cHour :: !(Maybe Int)
20 , w3cMinute :: !(Maybe Int)
21 , w3cSecond :: !(Maybe Pico)
22 , w3cTimeZone :: !(Maybe TimeZone)
24 deriving (Show, Eq, Typeable)
26 instance Convertible Day W3CDateTime where
28 = case toGregorian day of
29 (y, m, d) -> return W3CDateTime {
36 , w3cTimeZone = Nothing
39 fetch :: (Show a, Typeable a, Typeable b) =>
46 Nothing -> convError ("No " ++ name ++ " information in the given value") a
49 instance Convertible W3CDateTime Day where
51 = do let y = w3cYear w3c
52 m <- fetch "month" w3cMonth w3c
53 d <- fetch "day" w3cDay w3c
54 return (fromGregorian y m d)
56 instance Convertible ZonedTime W3CDateTime where
58 = let lt = zonedTimeToLocalTime zt
61 hms = localTimeOfDay lt
64 w3cYear = case toGregorian ymd of (y, _, _) -> y
65 , w3cMonth = Just (case toGregorian ymd of (_, m, _) -> m)
66 , w3cDay = Just (case toGregorian ymd of (_, _, d) -> d)
67 , w3cHour = Just (todHour hms)
68 , w3cMinute = Just (todMin hms)
69 , w3cSecond = Just (todSec hms)
70 , w3cTimeZone = Just tz
73 instance Convertible W3CDateTime ZonedTime where
75 = do day <- safeConvert w3c
76 tod <- do h <- fetch "hour" w3cHour w3c
77 m <- fetch "minute" w3cMinute w3c
78 s <- fetch "second" w3cSecond w3c
79 case makeTimeOfDayValid h m s of
80 Just tod -> return tod
81 Nothing -> convError "Invalid time of day" w3c
82 tz <- fetch "timezone" w3cTimeZone w3c
84 zonedTimeToLocalTime = LocalTime day tod