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,
43 date_format = "%-m/%-d/%Y"
46 -- | The format string for a base date in mm/dd/yyyy format. The
47 -- day/month are padded to two characters with zeros. This will
54 date_format_padded :: String
55 date_format_padded = "%0m/%0d/%Y"
58 -- | (Un)pickle a UTCTime without the time portion.
62 (to_date, from_date) `xpWrapMaybe` xpText
64 to_date :: String -> Maybe UTCTime
65 to_date = parseTime defaultTimeLocale date_format
67 from_date :: UTCTime -> String
68 from_date = formatTime defaultTimeLocale date_format
71 -- | (Un)pickle a UTCTime without the time portion. The day/month are
72 -- padded to two characters with zeros.
74 xp_date_padded :: PU UTCTime
76 (to_date, from_date) `xpWrapMaybe` xpText
78 to_date :: String -> Maybe UTCTime
79 to_date = parseTime defaultTimeLocale date_format_padded
81 from_date :: UTCTime -> String
82 from_date = formatTime defaultTimeLocale date_format_padded
86 -- | Format a number as a string using a comma as the thousands
91 -- >>> format_commas 0
93 -- >>> format_commas 10
95 -- >>> format_commas 100
97 -- >>> format_commas 1000
99 -- >>> format_commas 10000
101 -- >>> format_commas 100000
103 -- >>> format_commas 1000000
106 format_commas :: Int -> String
108 reverse (intercalate "," $ chunksOf 3 $ reverse $ show x)
110 -- | Parse \<Earnings\> from an 'AutoRaceResultsListing'. These are
111 -- essentially 'Int's, but they look like,
113 -- * \<Earnings\>336,826\</Earnings\>
115 -- * \<Earnings\>1,000,191\</Earnings\>
117 -- * \<Earnings\>TBA\</Earnings\>
119 xp_earnings :: PU (Maybe Int)
121 (to_earnings, from_earnings) `xpWrap` xpText
123 strip_commas :: String -> String
124 strip_commas = replace "," ""
126 to_earnings :: String -> Maybe Int
128 | s == "TBA" = Nothing
129 | otherwise = Just $ (read . strip_commas) s
131 from_earnings :: Maybe Int -> String
132 from_earnings Nothing = "TBA"
133 from_earnings (Just i) = format_commas i
136 -- | (Un)pickle a 'UTCTime' from a \<RaceDate\> element in an
137 -- 'AutoRaceResults' message.
141 -- * \<RaceDate\>6/1/2014 1:00:00 PM\</RaceDate\>
143 -- * \<RaceDate\>5/24/2014 2:45:00 PM\</RaceDate\>
145 xp_racedate :: PU UTCTime
147 (to_racedate, from_racedate) `xpWrapMaybe` xpText
149 format = date_format ++ " " ++ "%-I:%M:%S %p"
151 to_racedate :: String -> Maybe UTCTime
152 to_racedate = parseTime defaultTimeLocale format
154 from_racedate :: UTCTime -> String
155 from_racedate = formatTime defaultTimeLocale format
158 -- | (Un)pickle a UTCTime from a weather forecast's gamedate. Example
161 -- \<forecast gamedate=\"Monday, December 30th\"\>
163 -- When unpickling we get rid of the suffixes \"st\", \"nd\", \"rd\", and
164 -- \"th\". During pickling, we add them back based on the last digit
167 xp_gamedate :: PU UTCTime
169 (to_gamedate, from_gamedate) `xpWrapMaybe` xpText
171 format = "%A, %B %-d"
173 to_gamedate :: String -> Maybe UTCTime
175 parseTime defaultTimeLocale format s'
177 s' = case (reverse s) of
178 (c2:c1:cs) -> let suffix = [c1,c2]
185 _ -> s -- Unknown suffix, leave it alone.
186 _ -> s -- The String is less than two characters long,
190 from_gamedate :: UTCTime -> String
191 from_gamedate d = s ++ (suffix s)
193 s = formatTime defaultTimeLocale format d
195 suffix :: String -> String
209 -- | (Un)pickle a UTCTime without the date portion.
211 xp_time :: PU UTCTime
213 (to_time, from_time) `xpWrapMaybe` xpText
215 to_time :: String -> Maybe UTCTime
216 to_time = parseTime defaultTimeLocale time_format
218 from_time :: UTCTime -> String
219 from_time = formatTime defaultTimeLocale time_format
222 -- | (Un)pickle a UTCTime without the date portion, allowing for a
223 -- value of \"TBA\" (which gets translated to 'Nothing').
225 xp_tba_time :: PU (Maybe UTCTime)
227 (to_time, from_time) `xpWrap` xpText
229 to_time :: String -> Maybe UTCTime
231 | s == "TBA" = Nothing
232 | otherwise = parseTime defaultTimeLocale time_format s
234 from_time :: Maybe UTCTime -> String
235 from_time Nothing = "TBA"
236 from_time (Just t) = formatTime defaultTimeLocale time_format t
240 -- | (Un)pickle the \<time_stamp\> element format to/from a 'UTCTime'.
242 -- Example: \<time_stamp\> January 6, 2014, at 10:11 PM ET \</time_stamp\>
244 -- TSN doesn't provide a proper time zone name, so we assume that
245 -- it's always Eastern Standard Time. EST is UTC-5, so we
246 -- add/subtract 5 hours to convert to/from UTC.
248 xp_time_stamp :: PU UTCTime
250 (parse_time_stamp, from_time_stamp) `xpWrapMaybe` xpText
252 five_hours :: NominalDiffTime
253 five_hours = 5 * 60 * 60
255 subtract_five :: UTCTime -> UTCTime
256 subtract_five = addUTCTime (-1 * five_hours)
258 from_time_stamp :: UTCTime -> String
260 formatTime defaultTimeLocale time_stamp_format . subtract_five