1 -- | (Un)picklers for data types present in The Sports Network XML
16 import Data.List ( intercalate )
17 import Data.List.Split ( chunksOf )
18 import Data.String.Utils ( replace )
19 import Data.Time.Clock ( NominalDiffTime, UTCTime, addUTCTime )
20 import Data.Time.Format ( formatTime, parseTime )
21 import System.Locale ( defaultTimeLocale )
22 import Text.XML.HXT.Arrow.Pickle (
26 import Text.XML.HXT.Arrow.Pickle.Xml ( PU )
35 -- | The format string for a base date in m/d/yyyy format. The
36 -- day/month are not padded at all. This will match for example,
42 date_format = "%-m/%-d/%Y"
45 -- | The format string for a base date in mm/dd/yyyy format. The
46 -- day/month are padded to two characters with zeros. This will
52 date_format_padded :: String
53 date_format_padded = "%0m/%0d/%Y"
56 -- | (Un)pickle a UTCTime without the time portion.
60 (to_date, from_date) `xpWrapMaybe` xpText
62 to_date :: String -> Maybe UTCTime
63 to_date = parseTime defaultTimeLocale date_format
65 from_date :: UTCTime -> String
66 from_date = formatTime defaultTimeLocale date_format
69 -- | (Un)pickle a UTCTime without the time portion. The day/month are
70 -- padded to two characters with zeros.
72 xp_date_padded :: PU UTCTime
74 (to_date, from_date) `xpWrapMaybe` xpText
76 to_date :: String -> Maybe UTCTime
77 to_date = parseTime defaultTimeLocale date_format_padded
79 from_date :: UTCTime -> String
80 from_date = formatTime defaultTimeLocale date_format_padded
84 -- | Format a number as a string using a comma as the thousands
89 -- >>> format_commas 0
91 -- >>> format_commas 10
93 -- >>> format_commas 100
95 -- >>> format_commas 1000
97 -- >>> format_commas 10000
99 -- >>> format_commas 100000
101 -- >>> format_commas 1000000
104 format_commas :: Int -> String
106 reverse (intercalate "," $ chunksOf 3 $ reverse $ show x)
108 -- | Parse \<Earnings\> from an 'AutoRaceResultsListing'. These are
109 -- essentially 'Int's, but they look like,
111 -- * <Earnings>336,826</Earnings>
112 -- * <Earnings>1,000,191</Earnings>
113 -- * <Earnings>TBA</Earnings>
115 xp_earnings :: PU (Maybe Int)
117 (to_earnings, from_earnings) `xpWrap` xpText
119 strip_commas :: String -> String
120 strip_commas = replace "," ""
122 to_earnings :: String -> Maybe Int
124 | s == "TBA" = Nothing
125 | otherwise = (read . strip_commas . show) s
127 from_earnings :: Maybe Int -> String
128 from_earnings Nothing = "TBA"
129 from_earnings (Just i) = format_commas i
132 -- | (Un)pickle a 'UTCTime' from a \<RaceDate\> element in an
133 -- 'AutoRaceResults' message.
137 -- <RaceDate>6/1/2014 1:00:00 PM</RaceDate>
138 -- <RaceDate>5/24/2014 2:45:00 PM</RaceDate>
140 xp_racedate :: PU UTCTime
142 (to_racedate, from_racedate) `xpWrapMaybe` xpText
144 format = date_format ++ " " ++ "%-I:%M:%S %p"
146 to_racedate :: String -> Maybe UTCTime
147 to_racedate = parseTime defaultTimeLocale format
149 from_racedate :: UTCTime -> String
150 from_racedate = formatTime defaultTimeLocale format
153 -- | (Un)pickle a UTCTime from a weather forecast's gamedate. Example
156 -- \<forecast gamedate=\"Monday, December 30th\"\>
158 -- When unpickling we get rid of the suffixes \"st\", \"nd\", \"rd\", and
159 -- \"th\". During pickling, we add them back based on the last digit
162 xp_gamedate :: PU UTCTime
164 (to_gamedate, from_gamedate) `xpWrapMaybe` xpText
166 format = "%A, %B %-d"
168 to_gamedate :: String -> Maybe UTCTime
170 parseTime defaultTimeLocale format s'
172 s' = case (reverse s) of
173 (c2:c1:cs) -> let suffix = [c1,c2]
180 _ -> s -- Unknown suffix, leave it alone.
181 _ -> s -- The String is less than two characters long,
185 from_gamedate :: UTCTime -> String
186 from_gamedate d = s ++ (suffix s)
188 s = formatTime defaultTimeLocale format d
190 suffix :: String -> String
204 -- | (Un)pickle a UTCTime without the date portion.
206 xp_time :: PU UTCTime
208 (to_time, from_time) `xpWrapMaybe` xpText
210 to_time :: String -> Maybe UTCTime
211 to_time = parseTime defaultTimeLocale time_format
213 from_time :: UTCTime -> String
214 from_time = formatTime defaultTimeLocale time_format
217 -- | (Un)pickle a UTCTime without the date portion, allowing for a
218 -- value of \"TBA\" (which gets translated to 'Nothing').
220 xp_tba_time :: PU (Maybe UTCTime)
222 (to_time, from_time) `xpWrap` xpText
224 to_time :: String -> Maybe UTCTime
226 | s == "TBA" = Nothing
227 | otherwise = parseTime defaultTimeLocale time_format s
229 from_time :: Maybe UTCTime -> String
230 from_time Nothing = ""
231 from_time (Just t) = formatTime defaultTimeLocale time_format t
235 -- | (Un)pickle the \<time_stamp\> element format to/from a 'UTCTime'.
237 -- Example: \<time_stamp\> January 6, 2014, at 10:11 PM ET \</time_stamp\>
239 -- TSN doesn't provide a proper time zone name, so we assume that
240 -- it's always Eastern Standard Time. EST is UTC-5, so we
241 -- add/subtract 5 hours to convert to/from UTC.
243 xp_time_stamp :: PU UTCTime
245 (parse_time_stamp, from_time_stamp) `xpWrapMaybe` xpText
247 five_hours :: NominalDiffTime
248 five_hours = 5 * 60 * 60
250 subtract_five :: UTCTime -> UTCTime
251 subtract_five = addUTCTime (-1 * five_hours)
253 from_time_stamp :: UTCTime -> String
255 formatTime defaultTimeLocale time_stamp_format . subtract_five