1 -- | (Un)picklers for data types present in The Sports Network XML
15 import Data.Time.Clock ( NominalDiffTime, UTCTime, addUTCTime )
16 import Data.Time.Format ( formatTime, parseTime )
17 import System.Locale ( defaultTimeLocale )
18 import Text.XML.HXT.Arrow.Pickle (
22 import Text.XML.HXT.Arrow.Pickle.Xml ( PU )
31 -- | The format string for a base date in m/d/yyyy format. The
32 -- day/month are not padded at all. This will match for example,
38 date_format = "%-m/%-d/%Y"
41 -- | The format string for a base date in mm/dd/yyyy format. The
42 -- day/month are padded to two characters with zeros. This will
48 date_format_padded :: String
49 date_format_padded = "%0m/%0d/%Y"
52 -- | (Un)pickle a UTCTime without the time portion.
56 (to_date, from_date) `xpWrapMaybe` xpText
58 to_date :: String -> Maybe UTCTime
59 to_date = parseTime defaultTimeLocale date_format
61 from_date :: UTCTime -> String
62 from_date = formatTime defaultTimeLocale date_format
65 -- | (Un)pickle a UTCTime without the time portion. The day/month are
66 -- padded to two characters with zeros.
68 xp_date_padded :: PU UTCTime
70 (to_date, from_date) `xpWrapMaybe` xpText
72 to_date :: String -> Maybe UTCTime
73 to_date = parseTime defaultTimeLocale date_format_padded
75 from_date :: UTCTime -> String
76 from_date = formatTime defaultTimeLocale date_format_padded
79 -- | (Un)pickle a 'UTCTime' from a \<RaceDate\> element in an
80 -- 'AutoRaceResults' message.
84 -- <RaceDate>6/1/2014 1:00:00 PM</RaceDate>
85 -- <RaceDate>5/24/2014 2:45:00 PM</RaceDate>
87 xp_racedate :: PU UTCTime
89 (to_racedate, from_racedate) `xpWrapMaybe` xpText
91 format = date_format ++ " " ++ "%-I:%M:%S %p"
93 to_racedate :: String -> Maybe UTCTime
94 to_racedate = parseTime defaultTimeLocale format
96 from_racedate :: UTCTime -> String
97 from_racedate = formatTime defaultTimeLocale format
100 -- | (Un)pickle a UTCTime from a weather forecast's gamedate. Example
103 -- \<forecast gamedate=\"Monday, December 30th\"\>
105 -- When unpickling we get rid of the suffixes \"st\", \"nd\", \"rd\", and
106 -- \"th\". During pickling, we add them back based on the last digit
109 xp_gamedate :: PU UTCTime
111 (to_gamedate, from_gamedate) `xpWrapMaybe` xpText
113 format = "%A, %B %-d"
115 to_gamedate :: String -> Maybe UTCTime
117 parseTime defaultTimeLocale format s'
119 s' = case (reverse s) of
120 (c2:c1:cs) -> let suffix = [c1,c2]
127 _ -> s -- Unknown suffix, leave it alone.
128 _ -> s -- The String is less than two characters long,
132 from_gamedate :: UTCTime -> String
133 from_gamedate d = s ++ (suffix s)
135 s = formatTime defaultTimeLocale format d
137 suffix :: String -> String
151 -- | (Un)pickle a UTCTime without the date portion.
153 xp_time :: PU UTCTime
155 (to_time, from_time) `xpWrapMaybe` xpText
157 to_time :: String -> Maybe UTCTime
158 to_time = parseTime defaultTimeLocale time_format
160 from_time :: UTCTime -> String
161 from_time = formatTime defaultTimeLocale time_format
164 -- | (Un)pickle a UTCTime without the date portion, allowing for a
165 -- value of \"TBA\" (which gets translated to 'Nothing').
167 xp_tba_time :: PU (Maybe UTCTime)
169 (to_time, from_time) `xpWrap` xpText
171 to_time :: String -> Maybe UTCTime
173 | s == "TBA" = Nothing
174 | otherwise = parseTime defaultTimeLocale time_format s
176 from_time :: Maybe UTCTime -> String
177 from_time Nothing = ""
178 from_time (Just t) = formatTime defaultTimeLocale time_format t
182 -- | (Un)pickle the \<time_stamp\> element format to/from a 'UTCTime'.
184 -- Example: \<time_stamp\> January 6, 2014, at 10:11 PM ET \</time_stamp\>
186 -- TSN doesn't provide a proper time zone name, so we assume that
187 -- it's always Eastern Standard Time. EST is UTC-5, so we
188 -- add/subtract 5 hours to convert to/from UTC.
190 xp_time_stamp :: PU UTCTime
192 (parse_time_stamp, from_time_stamp) `xpWrapMaybe` xpText
194 five_hours :: NominalDiffTime
195 five_hours = 5 * 60 * 60
197 subtract_five :: UTCTime -> UTCTime
198 subtract_five = addUTCTime (-1 * five_hours)
200 from_time_stamp :: UTCTime -> String
202 formatTime defaultTimeLocale time_stamp_format . subtract_five