1 -- | (Un)picklers for data types present in The Sports Network XML
19 import Data.List ( intercalate )
20 import Data.List.Split ( chunksOf )
21 import Data.String.Utils ( replace )
22 import Data.Time.Clock ( NominalDiffTime, UTCTime, addUTCTime )
23 import Data.Time.Format ( formatTime, parseTime )
24 import Data.Tree.NTree.TypeDefs ( NTree(..) )
25 import System.Locale ( defaultTimeLocale )
26 import Test.Tasty ( TestTree, testGroup )
27 import Test.Tasty.HUnit ( (@?=), testCase )
28 import Text.XML.HXT.Arrow.Pickle (
32 import Text.XML.HXT.Arrow.Pickle.Xml ( PU )
33 import Text.XML.HXT.Core (
47 -- | The format string for a base date in m/d/yyyy format. The
48 -- day/month are not padded at all. This will match for example,
55 date_format = "%-m/%-d/%Y"
58 -- | The format string for a base date in mm/dd/yyyy format. The
59 -- day/month are padded to two characters with zeros. This will
66 date_format_padded :: String
67 date_format_padded = "%0m/%0d/%Y"
70 -- | (Un)pickle a UTCTime without the time portion.
76 -- >>> let tn = text_node "2/15/1983"
77 -- >>> unpickleDoc xp_date tn
78 -- Just 1983-02-15 00:00:00 UTC
80 -- But for some reason, it can also parse a leading zero in the
81 -- month. Whatever. This isn't required behavior.
83 -- >>> let tn = text_node "02/15/1983"
84 -- >>> unpickleDoc xp_date tn
85 -- Just 1983-02-15 00:00:00 UTC
89 (to_date, from_date) `xpWrapMaybe` xpText
91 to_date :: String -> Maybe UTCTime
92 to_date = parseTime defaultTimeLocale date_format
94 from_date :: UTCTime -> String
95 from_date = formatTime defaultTimeLocale date_format
98 -- | (Un)pickle a UTCTime without the time portion. The day/month are
99 -- padded to two characters with zeros.
103 -- >>> let tn = text_node "02/15/1983"
104 -- >>> unpickleDoc xp_date_padded tn
105 -- Just 1983-02-15 00:00:00 UTC
107 xp_date_padded :: PU UTCTime
109 (to_date, from_date) `xpWrapMaybe` xpText
111 to_date :: String -> Maybe UTCTime
112 to_date = parseTime defaultTimeLocale date_format_padded
114 from_date :: UTCTime -> String
115 from_date = formatTime defaultTimeLocale date_format_padded
119 -- | Format a number as a string using a comma as the thousands
124 -- >>> format_commas 0
126 -- >>> format_commas 10
128 -- >>> format_commas 100
130 -- >>> format_commas 1000
132 -- >>> format_commas 10000
134 -- >>> format_commas 100000
136 -- >>> format_commas 1000000
139 format_commas :: Int -> String
141 reverse (intercalate "," $ chunksOf 3 $ reverse $ show x)
144 -- | Parse \<Earnings\> from an 'AutoRaceResultsListing'. These are
145 -- essentially 'Int's, but they look like,
147 -- * \<Earnings\>336,826\</Earnings\>
149 -- * \<Earnings\>1,000,191\</Earnings\>
151 -- * \<Earnings\>TBA\</Earnings\>
155 -- >>> let tn = text_node "1,000,191"
156 -- >>> unpickleDoc xp_earnings tn
157 -- Just (Just 1000191)
159 -- >>> let tn = text_node "TBA"
160 -- >>> unpickleDoc xp_earnings tn
163 xp_earnings :: PU (Maybe Int)
165 (to_earnings, from_earnings) `xpWrap` xpText
167 strip_commas :: String -> String
168 strip_commas = replace "," ""
170 to_earnings :: String -> Maybe Int
172 | s == "TBA" = Nothing
173 | otherwise = Just $ (read . strip_commas) s
175 from_earnings :: Maybe Int -> String
176 from_earnings Nothing = "TBA"
177 from_earnings (Just i) = format_commas i
181 -- | (Un)pickle an unpadded 'UTCTime'. Used for example on the
182 -- \<RaceDate\> elements in an 'AutoRaceResults' message.
186 -- >>> let tn = text_node "6/1/2014 1:00:00 PM"
187 -- >>> unpickleDoc xp_datetime tn
188 -- Just 2014-06-01 13:00:00 UTC
190 -- >>> let tn = text_node "5/24/2014 2:45:00 PM"
191 -- >>> unpickleDoc xp_datetime tn
192 -- Just 2014-05-24 14:45:00 UTC
194 -- Padded! For some reason it works with only one zero in front. I
195 -- dunno man. NOT required (or even desired?) behavior.
197 -- >>> let tn = text_node "05/24/2014 2:45:00 PM"
198 -- >>> unpickleDoc xp_datetime tn
199 -- Just 2014-05-24 14:45:00 UTC
201 xp_datetime :: PU UTCTime
203 (to_datetime, from_datetime) `xpWrapMaybe` xpText
205 format = date_format ++ " " ++ "%-I:%M:%S %p"
207 to_datetime :: String -> Maybe UTCTime
208 to_datetime = parseTime defaultTimeLocale format
210 from_datetime :: UTCTime -> String
211 from_datetime = formatTime defaultTimeLocale format
214 -- | (Un)pickle a UTCTime from a weather forecast's gamedate. Example
217 -- When unpickling we get rid of the suffixes \"st\", \"nd\", \"rd\", and
218 -- \"th\". During pickling, we add them back based on the last digit
223 -- >>> let tn = text_node "Monday, December 30th"
224 -- >>> let (Just gd) = unpickleDoc xp_gamedate tn
226 -- 1970-12-30 00:00:00 UTC
227 -- >>> pickleDoc xp_gamedate gd
228 -- NTree (XTag "/" []) [NTree (XText "Wednesday, December 30th") []]
230 xp_gamedate :: PU UTCTime
232 (to_gamedate, from_gamedate) `xpWrapMaybe` xpText
234 format = "%A, %B %-d"
236 to_gamedate :: String -> Maybe UTCTime
238 parseTime defaultTimeLocale format s'
240 s' = case (reverse s) of
241 (c2:c1:cs) -> let suffix = [c1,c2]
248 _ -> s -- Unknown suffix, leave it alone.
249 _ -> s -- The String is less than two characters long,
253 from_gamedate :: UTCTime -> String
254 from_gamedate d = s ++ (suffix s)
256 s = formatTime defaultTimeLocale format d
258 suffix :: String -> String
272 -- | (Un)pickle a UTCTime without the date portion. Doesn't work if
273 -- the fields aren't zero-padded to two characters.
277 -- Padded, should work:
279 -- >>> let tn = text_node "04:35 PM"
280 -- >>> unpickleDoc xp_time tn
281 -- Just 1970-01-01 16:35:00 UTC
283 -- Unpadded, should fail:
285 -- >>> let tn = text_node "4:35 PM"
286 -- >>> unpickleDoc xp_time tn
289 xp_time :: PU UTCTime
291 (to_time, from_time) `xpWrapMaybe` xpText
293 to_time :: String -> Maybe UTCTime
294 to_time = parseTime defaultTimeLocale time_format
296 from_time :: UTCTime -> String
297 from_time = formatTime defaultTimeLocale time_format
300 -- | (Un)pickle a UTCTime without the date portion. This differs from
301 -- 'xp_time' in that it uses periods in the AM/PM part, i.e. \"A.M.\"
302 -- and \"P.M.\" It also doesn't use padding for the \"hours\" part.
306 -- A standard example of the correct form:
308 -- >>> let tn = text_node "11:30 A.M."
309 -- >>> let (Just result) = unpickleDoc xp_time_dots tn
311 -- 1970-01-01 11:30:00 UTC
312 -- >>> pickleDoc xp_time_dots result
313 -- NTree (XTag "/" []) [NTree (XText "11:30 A.M.") []]
315 -- Another miracle, it still parses with a leading zero!
317 -- >>> let tn = text_node "01:30 A.M."
318 -- >>> unpickleDoc xp_time_dots tn
319 -- Just 1970-01-01 01:30:00 UTC
321 xp_time_dots :: PU UTCTime
323 (to_time, from_time) `xpWrapMaybe` xpText
325 -- | The hours arent padded with zeros.
326 nopad_time_format :: String
327 nopad_time_format = "%-I:%M %p"
329 to_time :: String -> Maybe UTCTime
330 to_time = (parseTime defaultTimeLocale nopad_time_format) . (replace "." "")
332 from_time :: UTCTime -> String
334 replace "AM" "A.M." (replace "PM" "P.M." s)
336 s = formatTime defaultTimeLocale nopad_time_format t
339 -- | (Un)pickle a UTCTime without the date portion, allowing for a
340 -- value of \"TBA\" (which gets translated to 'Nothing').
344 -- A failed parse will return 'Nothing':
346 -- >>> let tn = text_node "YO"
347 -- >>> unpickleDoc xp_tba_time tn
350 -- And so will parsing a \"TBA\":
352 -- >>> let tn = text_node "TBA"
353 -- >>> unpickleDoc xp_tba_time tn
356 -- But re-pickling 'Nothing' gives only \"TBA\":
358 -- >>> pickleDoc xp_tba_time Nothing
359 -- NTree (XTag "/" []) [NTree (XText "TBA") []]
361 -- A normal time is also parsed successfully, of course:
363 -- >>> let tn = text_node "08:10 PM"
364 -- >>> unpickleDoc xp_tba_time tn
365 -- Just (Just 1970-01-01 20:10:00 UTC)
367 xp_tba_time :: PU (Maybe UTCTime)
369 (to_time, from_time) `xpWrap` xpText
371 to_time :: String -> Maybe UTCTime
373 | s == "TBA" = Nothing
374 | otherwise = parseTime defaultTimeLocale time_format s
376 from_time :: Maybe UTCTime -> String
377 from_time Nothing = "TBA"
378 from_time (Just t) = formatTime defaultTimeLocale time_format t
382 -- | (Un)pickle the \<time_stamp\> element format to/from a 'UTCTime'.
383 -- The time_stamp elements look something like,
385 -- \<time_stamp\> January 6, 2014, at 10:11 PM ET \</time_stamp\>
387 -- TSN doesn't provide a proper time zone name, so we assume that
388 -- it's always Eastern Standard Time. EST is UTC-5, so we
389 -- add/subtract 5 hours to convert to/from UTC.
393 -- >>> let tn = text_node " January 6, 2014, at 10:11 PM ET "
394 -- >>> unpickleDoc xp_time_stamp tn
395 -- Just 2014-01-07 03:11:00 UTC
397 xp_time_stamp :: PU UTCTime
399 (parse_time_stamp, from_time_stamp) `xpWrapMaybe` xpText
401 five_hours :: NominalDiffTime
402 five_hours = 5 * 60 * 60
404 subtract_five :: UTCTime -> UTCTime
405 subtract_five = addUTCTime (-1 * five_hours)
407 from_time_stamp :: UTCTime -> String
409 formatTime defaultTimeLocale time_stamp_format . subtract_five
412 -- | (Un)pickle an ambiguous 12-hour AM/PM time, which is ambiguous
413 -- because it's missing the AM/PM part.
417 -- >>> let tn = text_node "8:00"
418 -- >>> unpickleDoc xp_ambiguous_time tn
419 -- Just 1970-01-01 08:00:00 UTC
421 xp_ambiguous_time :: PU UTCTime
423 (to_time, from_time) `xpWrapMaybe` xpText
425 ambiguous_time_format :: String
426 ambiguous_time_format = "%-I:%M"
428 to_time :: String -> Maybe UTCTime
429 to_time = parseTime defaultTimeLocale ambiguous_time_format
431 from_time :: UTCTime -> String
433 formatTime defaultTimeLocale ambiguous_time_format
436 -- | Create an 'XmlTree' containing only the given text. This is
437 -- useful for testing (un)picklers, where we don't want to have to
438 -- bother to create a dummy XML document.
442 -- >>> text_node "8:00"
443 -- NTree (XText "8:00") []
445 text_node :: String -> XmlTree
446 text_node s = NTree (XText s) []
454 -- | A list of all tests for this module. This primary exists to
455 -- eliminate the unused import/export warnings for 'unpickleDoc' and
456 -- 'text_node' which are otherwise only used in the doctests.
458 pickler_tests :: TestTree
462 [ test_pickle_of_unpickle_is_identity ]
465 -- | If we unpickle something and then pickle it, we should wind up
466 -- with the same thing we started with (plus an additional root
469 test_pickle_of_unpickle_is_identity :: TestTree
470 test_pickle_of_unpickle_is_identity =
471 testCase "pickle composed with unpickle is (almost) the identity" $ do
472 let tn = text_node "8:00"
473 let (Just utctime) = unpickleDoc xp_ambiguous_time tn
474 let actual = pickleDoc xp_ambiguous_time utctime
475 let expected = NTree (XTag (mkName "/") []) [tn]