1 -- | (Un)picklers for data types present in The Sports Network XML
16 import Data.String.Utils ( replace )
17 import Data.Time.Clock ( NominalDiffTime, UTCTime, addUTCTime )
18 import Data.Time.Format ( formatTime, parseTime )
19 import System.Locale ( defaultTimeLocale )
20 import Text.XML.HXT.Arrow.Pickle (
24 import Text.XML.HXT.Arrow.Pickle.Xml ( PU )
33 -- | The format string for a base date in m/d/yyyy format. The
34 -- day/month are not padded at all. This will match for example,
40 date_format = "%-m/%-d/%Y"
43 -- | The format string for a base date in mm/dd/yyyy format. The
44 -- day/month are padded to two characters with zeros. This will
50 date_format_padded :: String
51 date_format_padded = "%0m/%0d/%Y"
54 -- | (Un)pickle a UTCTime without the time portion.
58 (to_date, from_date) `xpWrapMaybe` xpText
60 to_date :: String -> Maybe UTCTime
61 to_date = parseTime defaultTimeLocale date_format
63 from_date :: UTCTime -> String
64 from_date = formatTime defaultTimeLocale date_format
67 -- | (Un)pickle a UTCTime without the time portion. The day/month are
68 -- padded to two characters with zeros.
70 xp_date_padded :: PU UTCTime
72 (to_date, from_date) `xpWrapMaybe` xpText
74 to_date :: String -> Maybe UTCTime
75 to_date = parseTime defaultTimeLocale date_format_padded
77 from_date :: UTCTime -> String
78 from_date = formatTime defaultTimeLocale date_format_padded
81 -- | Parse \<Earnings\> from an 'AutoRaceResultsListing'. These are
82 -- essentially 'Int's, but they look like,
84 -- * <Earnings>336,826</Earnings>
85 -- * <Earnings>1,000,191</Earnings>
86 -- * <Earnings>TBA</Earnings>
88 xp_earnings :: PU (Maybe Int)
90 (to_earnings, from_earnings) `xpWrap` xpText
92 strip_commas :: String -> String
93 strip_commas = replace "," ""
95 to_earnings :: String -> Maybe Int
97 | s == "TBA" = Nothing
98 | otherwise = (read . strip_commas . show) s
100 from_earnings :: Maybe Int -> String
101 from_earnings Nothing = ""
102 from_earnings (Just i) = show i
105 -- | (Un)pickle a 'UTCTime' from a \<RaceDate\> element in an
106 -- 'AutoRaceResults' message.
110 -- <RaceDate>6/1/2014 1:00:00 PM</RaceDate>
111 -- <RaceDate>5/24/2014 2:45:00 PM</RaceDate>
113 xp_racedate :: PU UTCTime
115 (to_racedate, from_racedate) `xpWrapMaybe` xpText
117 format = date_format ++ " " ++ "%-I:%M:%S %p"
119 to_racedate :: String -> Maybe UTCTime
120 to_racedate = parseTime defaultTimeLocale format
122 from_racedate :: UTCTime -> String
123 from_racedate = formatTime defaultTimeLocale format
126 -- | (Un)pickle a UTCTime from a weather forecast's gamedate. Example
129 -- \<forecast gamedate=\"Monday, December 30th\"\>
131 -- When unpickling we get rid of the suffixes \"st\", \"nd\", \"rd\", and
132 -- \"th\". During pickling, we add them back based on the last digit
135 xp_gamedate :: PU UTCTime
137 (to_gamedate, from_gamedate) `xpWrapMaybe` xpText
139 format = "%A, %B %-d"
141 to_gamedate :: String -> Maybe UTCTime
143 parseTime defaultTimeLocale format s'
145 s' = case (reverse s) of
146 (c2:c1:cs) -> let suffix = [c1,c2]
153 _ -> s -- Unknown suffix, leave it alone.
154 _ -> s -- The String is less than two characters long,
158 from_gamedate :: UTCTime -> String
159 from_gamedate d = s ++ (suffix s)
161 s = formatTime defaultTimeLocale format d
163 suffix :: String -> String
177 -- | (Un)pickle a UTCTime without the date portion.
179 xp_time :: PU UTCTime
181 (to_time, from_time) `xpWrapMaybe` xpText
183 to_time :: String -> Maybe UTCTime
184 to_time = parseTime defaultTimeLocale time_format
186 from_time :: UTCTime -> String
187 from_time = formatTime defaultTimeLocale time_format
190 -- | (Un)pickle a UTCTime without the date portion, allowing for a
191 -- value of \"TBA\" (which gets translated to 'Nothing').
193 xp_tba_time :: PU (Maybe UTCTime)
195 (to_time, from_time) `xpWrap` xpText
197 to_time :: String -> Maybe UTCTime
199 | s == "TBA" = Nothing
200 | otherwise = parseTime defaultTimeLocale time_format s
202 from_time :: Maybe UTCTime -> String
203 from_time Nothing = ""
204 from_time (Just t) = formatTime defaultTimeLocale time_format t
208 -- | (Un)pickle the \<time_stamp\> element format to/from a 'UTCTime'.
210 -- Example: \<time_stamp\> January 6, 2014, at 10:11 PM ET \</time_stamp\>
212 -- TSN doesn't provide a proper time zone name, so we assume that
213 -- it's always Eastern Standard Time. EST is UTC-5, so we
214 -- add/subtract 5 hours to convert to/from UTC.
216 xp_time_stamp :: PU UTCTime
218 (parse_time_stamp, from_time_stamp) `xpWrapMaybe` xpText
220 five_hours :: NominalDiffTime
221 five_hours = 5 * 60 * 60
223 subtract_five :: UTCTime -> UTCTime
224 subtract_five = addUTCTime (-1 * five_hours)
226 from_time_stamp :: UTCTime -> String
228 formatTime defaultTimeLocale time_stamp_format . subtract_five