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 UTCTime W3CDateTime where
58 = let (y, m, d) = toGregorian (utctDay u)
59 hms = timeToTimeOfDay (utctDayTime u)
65 , w3cHour = Just (todHour hms)
66 , w3cMinute = Just (todMin hms)
67 , w3cSecond = Just (todSec hms)
68 , w3cTimeZone = Just utc
71 instance Convertible W3CDateTime UTCTime where
73 = do day <- safeConvert w3c
74 tod <- do h <- fetch "hour" w3cHour w3c
75 m <- fetch "minute" w3cMinute w3c
76 s <- fetch "second" w3cSecond w3c
77 case makeTimeOfDayValid h m s of
78 Just tod -> return tod
79 Nothing -> convError "Invalid time of day" w3c
80 tz <- fetch "timezone" w3cTimeZone w3c
81 let lt = LocalTime day tod
82 return (localTimeToUTC tz lt)