1 -- | (Un)picklers for data types present in The Sports Network XML
12 xp_fracpart_only_double,
21 import Data.Char ( toUpper )
22 import Data.List ( intercalate )
23 import Data.List.Split ( chunksOf )
24 import Data.Maybe ( catMaybes, listToMaybe )
25 import Data.String.Utils ( replace )
26 import Data.Time.Clock ( UTCTime )
27 import Data.Time.Format ( formatTime, parseTime )
28 import Data.Tree.NTree.TypeDefs ( NTree(..) )
29 import System.Locale ( TimeLocale( wDays, months ), defaultTimeLocale )
30 import Test.Tasty ( TestTree, testGroup )
31 import Test.Tasty.HUnit ( (@?=), testCase )
32 import Text.Read ( readMaybe )
33 import Text.XML.HXT.Arrow.Pickle (
37 import Text.XML.HXT.Arrow.Pickle.Xml ( PU )
38 import Text.XML.HXT.Core (
52 -- | The format string for a base date in m/d/yyyy format. The
53 -- day/month are not padded at all. This will match for example,
60 date_format = "%-m/%-d/%Y"
63 -- | The format string for a base date in mm/dd/yyyy format. The
64 -- day/month are padded to two characters with zeros. This will
71 date_format_padded :: String
72 date_format_padded = "%0m/%0d/%Y"
75 -- | (Un)pickle a UTCTime without the time portion.
81 -- >>> let tn = text_node "2/15/1983"
82 -- >>> unpickleDoc xp_date tn
83 -- Just 1983-02-15 00:00:00 UTC
85 -- But for some reason, it can also parse a leading zero in the
86 -- month. Whatever. This isn't required behavior.
88 -- >>> let tn = text_node "02/15/1983"
89 -- >>> unpickleDoc xp_date tn
90 -- Just 1983-02-15 00:00:00 UTC
94 (to_date, from_date) `xpWrapMaybe` xpText
96 to_date :: String -> Maybe UTCTime
97 to_date = parseTime defaultTimeLocale date_format
99 from_date :: UTCTime -> String
100 from_date = formatTime defaultTimeLocale date_format
103 -- | (Un)pickle a UTCTime without the time portion. The day/month are
104 -- padded to two characters with zeros.
108 -- >>> let tn = text_node "02/15/1983"
109 -- >>> unpickleDoc xp_date_padded tn
110 -- Just 1983-02-15 00:00:00 UTC
112 -- >>> let tn = text_node "06/07/2014"
113 -- >>> unpickleDoc xp_date_padded tn
114 -- Just 2014-06-07 00:00:00 UTC
116 xp_date_padded :: PU UTCTime
118 (to_date, from_date) `xpWrapMaybe` xpText
120 to_date :: String -> Maybe UTCTime
121 to_date = parseTime defaultTimeLocale date_format_padded
123 from_date :: UTCTime -> String
124 from_date = formatTime defaultTimeLocale date_format_padded
128 -- | Format a number as a string using a comma as the thousands
133 -- >>> format_commas 0
135 -- >>> format_commas 10
137 -- >>> format_commas 100
139 -- >>> format_commas 1000
141 -- >>> format_commas 10000
143 -- >>> format_commas 100000
145 -- >>> format_commas 1000000
148 format_commas :: Int -> String
150 reverse (intercalate "," $ chunksOf 3 $ reverse $ show x)
154 -- | Parse \<Earnings\> from an 'AutoRaceResultsListing'. These are
155 -- essentially 'Int's, but they look like,
157 -- * \<Earnings\>336,826\</Earnings\>
159 -- * \<Earnings\>1,000,191\</Earnings\>
161 -- * \<Earnings\>TBA\</Earnings\>
165 -- >>> let tn = text_node "1,000,191"
166 -- >>> unpickleDoc xp_earnings tn
167 -- Just (Just 1000191)
169 -- >>> let tn = text_node "TBA"
170 -- >>> unpickleDoc xp_earnings tn
173 xp_earnings :: PU (Maybe Int)
175 (to_earnings, from_earnings) `xpWrap` xpText
177 strip_commas :: String -> String
178 strip_commas = replace "," ""
180 to_earnings :: String -> Maybe Int
182 | s == "TBA" = Nothing
183 | otherwise = Just $ (read . strip_commas) s
185 from_earnings :: Maybe Int -> String
186 from_earnings Nothing = "TBA"
187 from_earnings (Just i) = format_commas i
191 -- | Pickle a 'Double' that can be missing its leading zero (for
192 -- values less than one). For example, we've seen,
194 -- <TrackLength KPH=".805">0.5</TrackLength>
196 -- Which 'xpPrim' can't handle without the leading
197 -- zero. Unfortunately there's no way pickle/unpickle can be
198 -- inverses of each other here, since \"0.5\" and \".5\" should
199 -- unpickle to the same 'Double'.
203 -- >>> let tn = text_node "0.5"
204 -- >>> unpickleDoc xp_fracpart_only_double tn
207 -- >>> let tn = text_node ".5"
208 -- >>> unpickleDoc xp_fracpart_only_double tn
211 -- >>> let tn = text_node "foo"
212 -- >>> unpickleDoc xp_fracpart_only_double tn
215 xp_fracpart_only_double :: PU Double
216 xp_fracpart_only_double =
217 (to_double, from_double) `xpWrapMaybe` xpText
219 -- | Convert a 'String' to a 'Double', maybe. We always prepend a
220 -- zero, since it will fix the fraction-only values, and not hurt
221 -- the ones that already have a leading integer.
222 to_double :: String -> Maybe Double
223 to_double s = readMaybe ("0" ++ s)
225 from_double :: Double -> String
230 -- | (Un)pickle an unpadded 'UTCTime'. Used for example on the
231 -- \<RaceDate\> elements in an 'AutoRaceResults' message.
235 -- >>> let tn = text_node "6/1/2014 1:00:00 PM"
236 -- >>> unpickleDoc xp_datetime tn
237 -- Just 2014-06-01 13:00:00 UTC
239 -- >>> let tn = text_node "5/24/2014 2:45:00 PM"
240 -- >>> unpickleDoc xp_datetime tn
241 -- Just 2014-05-24 14:45:00 UTC
243 -- Padded! For some reason it works with only one zero in front. I
244 -- dunno man. NOT required (or even desired?) behavior.
246 -- >>> let tn = text_node "05/24/2014 2:45:00 PM"
247 -- >>> unpickleDoc xp_datetime tn
248 -- Just 2014-05-24 14:45:00 UTC
250 xp_datetime :: PU UTCTime
252 (to_datetime, from_datetime) `xpWrapMaybe` xpText
254 format = date_format ++ " " ++ "%-I:%M:%S %p"
256 to_datetime :: String -> Maybe UTCTime
257 to_datetime = parseTime defaultTimeLocale format
259 from_datetime :: UTCTime -> String
260 from_datetime = formatTime defaultTimeLocale format
264 -- | Takes a 'UTCTime', and returns the English suffix that would be
265 -- appropriate after the day of the month. For example, if we have a
266 -- UTCTime representing Christmas, this would return \"th\" because
267 -- \"th\" is the right suffix of \"December 25th\".
271 -- >>> import Data.Maybe ( fromJust )
273 -- let parse_date :: String -> Maybe UTCTime;
274 -- parse_date = parseTime defaultTimeLocale date_format;
277 -- >>> let dates = [ "1/" ++ (d : "/1970") | d <- ['1'..'9'] ]
278 -- >>> let suffixes = map (date_suffix . fromJust . parse_date) dates
280 -- ["st","nd","rd","th","th","th","th","th","th"]
282 date_suffix :: UTCTime -> String
284 case (reverse daystr) of
291 daystr = formatTime defaultTimeLocale "%d" t
294 -- | (Un)pickle a UTCTime from a weather forecast's gamedate. Example
297 -- When unpickling we get rid of the suffixes \"st\", \"nd\", \"rd\", and
298 -- \"th\". During pickling, we add them back based on the last digit
303 -- >>> let tn = text_node "Monday, December 30th"
304 -- >>> let (Just gd) = unpickleDoc xp_gamedate tn
306 -- 1970-12-30 00:00:00 UTC
307 -- >>> pickleDoc xp_gamedate gd
308 -- NTree (XTag "/" []) [NTree (XText "Wednesday, December 30th") []]
310 xp_gamedate :: PU UTCTime
312 (to_gamedate, from_gamedate) `xpWrapMaybe` xpText
314 format = "%A, %B %-d"
316 to_gamedate :: String -> Maybe UTCTime
318 parseTime defaultTimeLocale format s'
320 s' = case (reverse s) of
321 (c2:c1:cs) -> let suffix = [c1,c2]
323 if suffix `elem` ["st","nd","rd","th"]
325 else s -- Unknown suffix, leave it alone.
327 _ -> s -- The String is less than two characters long,
331 from_gamedate :: UTCTime -> String
332 from_gamedate d = s ++ (date_suffix d)
334 s = formatTime defaultTimeLocale format d
342 -- | (Un)pickle a UTCTime without the date portion. Doesn't work if
343 -- the fields aren't zero-padded to two characters.
347 -- Padded, should work:
349 -- >>> let tn = text_node "04:35 PM"
350 -- >>> unpickleDoc xp_time tn
351 -- Just 1970-01-01 16:35:00 UTC
353 -- Unpadded, should fail:
355 -- >>> let tn = text_node "4:35 PM"
356 -- >>> unpickleDoc xp_time tn
359 xp_time :: PU UTCTime
361 (to_time, from_time) `xpWrapMaybe` xpText
363 to_time :: String -> Maybe UTCTime
364 to_time = parseTime defaultTimeLocale time_format
366 from_time :: UTCTime -> String
367 from_time = formatTime defaultTimeLocale time_format
370 -- | (Un)pickle a UTCTime without the date portion. This differs from
371 -- 'xp_time' in that it uses periods in the AM/PM part, i.e. \"A.M.\"
372 -- and \"P.M.\" It also doesn't use padding for the \"hours\" part.
376 -- A standard example of the correct form:
378 -- >>> let tn = text_node "11:30 A.M."
379 -- >>> let (Just result) = unpickleDoc xp_time_dots tn
381 -- 1970-01-01 11:30:00 UTC
382 -- >>> pickleDoc xp_time_dots result
383 -- NTree (XTag "/" []) [NTree (XText "11:30 A.M.") []]
385 -- Another miracle, it still parses with a leading zero!
387 -- >>> let tn = text_node "01:30 A.M."
388 -- >>> unpickleDoc xp_time_dots tn
389 -- Just 1970-01-01 01:30:00 UTC
391 xp_time_dots :: PU UTCTime
393 (to_time, from_time) `xpWrapMaybe` xpText
395 -- | The hours arent padded with zeros.
396 nopad_time_format :: String
397 nopad_time_format = "%-I:%M %p"
399 to_time :: String -> Maybe UTCTime
400 to_time = (parseTime defaultTimeLocale nopad_time_format) . (replace "." "")
402 from_time :: UTCTime -> String
404 replace "AM" "A.M." (replace "PM" "P.M." s)
406 s = formatTime defaultTimeLocale nopad_time_format t
409 -- | (Un)pickle a UTCTime without the date portion, allowing for a
410 -- value of \"TBA\" (which gets translated to 'Nothing').
414 -- A failed parse will return 'Nothing':
416 -- >>> let tn = text_node "YO"
417 -- >>> unpickleDoc xp_tba_time tn
420 -- And so will parsing a \"TBA\":
422 -- >>> let tn = text_node "TBA"
423 -- >>> unpickleDoc xp_tba_time tn
426 -- But re-pickling 'Nothing' gives only \"TBA\":
428 -- >>> pickleDoc xp_tba_time Nothing
429 -- NTree (XTag "/" []) [NTree (XText "TBA") []]
431 -- A normal time is also parsed successfully, of course:
433 -- >>> let tn = text_node "08:10 PM"
434 -- >>> unpickleDoc xp_tba_time tn
435 -- Just (Just 1970-01-01 20:10:00 UTC)
437 xp_tba_time :: PU (Maybe UTCTime)
439 (to_time, from_time) `xpWrap` xpText
441 to_time :: String -> Maybe UTCTime
443 | s == "TBA" = Nothing
444 | otherwise = parseTime defaultTimeLocale time_format s
446 from_time :: Maybe UTCTime -> String
447 from_time Nothing = "TBA"
448 from_time (Just t) = formatTime defaultTimeLocale time_format t
452 -- | (Un)pickle the \<time_stamp\> element format to/from a 'UTCTime'.
453 -- The time_stamp elements look something like,
455 -- \<time_stamp\> January 6, 2014, at 10:11 PM ET \</time_stamp\>
457 -- TSN doesn't provide a proper time zone name, only \"ET\" for
458 -- \"Eastern Time\". But \"Eastern Time\" changes throughout the
459 -- year, depending on one's location, for daylight-savings
460 -- time. It's really not any more useful to be off by one hour than
461 -- it is to be off by 5 hours, so rather than guess at EDT/EST, we
462 -- just store the timestamp as UTC.
466 -- >>> let tn = text_node " January 6, 2014, at 10:11 PM ET "
467 -- >>> let (Just tstamp) = unpickleDoc xp_time_stamp tn
469 -- 2014-01-06 22:11:00 UTC
470 -- >>> pickleDoc xp_time_stamp tstamp
471 -- NTree (XTag "/" []) [NTree (XText " January 6, 2014, at 10:11 PM ET ") []]
473 xp_time_stamp :: PU UTCTime
475 (parse_time_stamp, from_time_stamp) `xpWrapMaybe` xpText
477 from_time_stamp :: UTCTime -> String
479 formatTime defaultTimeLocale time_stamp_format
483 -- | (Un)pickle an ambiguous 12-hour AM/PM time, which is ambiguous
484 -- because it's missing the AM/PM part.
488 -- >>> let tn = text_node "8:00"
489 -- >>> unpickleDoc xp_ambiguous_time tn
490 -- Just 1970-01-01 08:00:00 UTC
492 xp_ambiguous_time :: PU UTCTime
494 (to_time, from_time) `xpWrapMaybe` xpText
496 ambiguous_time_format :: String
497 ambiguous_time_format = "%-I:%M"
499 to_time :: String -> Maybe UTCTime
500 to_time = parseTime defaultTimeLocale ambiguous_time_format
502 from_time :: UTCTime -> String
504 formatTime defaultTimeLocale ambiguous_time_format
507 -- | Pickle a date value from a \<date\> element as they appear in the
508 -- early lines. This is a particularly wacky format, but then so is
509 -- the associated time (see 'xp_ambiguous_time').
513 -- >>> let tn = text_node "SUNDAY, MAY 25TH (05/25/2014)"
514 -- >>> let (Just result) = unpickleDoc xp_early_line_date tn
516 -- 2014-05-25 00:00:00 UTC
517 -- >>> pickleDoc xp_early_line_date result
518 -- NTree (XTag "/" []) [NTree (XText "SUNDAY, MAY 25TH (05/25/2014)") []]
520 -- >>> let tn = text_node "SATURDAY, JUNE 7TH (06/07/2014)"
521 -- >>> let (Just result) = unpickleDoc xp_early_line_date tn
523 -- 2014-06-07 00:00:00 UTC
524 -- >>> pickleDoc xp_early_line_date result
525 -- NTree (XTag "/" []) [NTree (XText "SATURDAY, JUNE 7TH (06/07/2014)") []]
527 xp_early_line_date :: PU UTCTime
529 (to_time, from_time) `xpWrapMaybe` xpText
531 -- | We need to create our own time locale that talks IN ALL CAPS.
532 -- Actually, 'parseTime' doesn't seem to care about the
533 -- case. But when we spit it back out again ('formatTime'),
534 -- we'll want it to be in all caps.
536 caps_time_locale :: TimeLocale
538 defaultTimeLocale { wDays = caps_days, months = caps_months }
540 caps_days :: [(String,String)]
541 caps_days = map both_to_upper (wDays defaultTimeLocale)
543 caps_months :: [(String,String)]
544 caps_months = map both_to_upper (months defaultTimeLocale)
546 both_to_upper :: (String,String) -> (String,String)
547 both_to_upper (s1,s2) = (map toUpper s1, map toUpper s2)
549 wacko_date_formats :: [String]
551 ["%A, %B %-d" ++ suffix ++ " (" ++ date_format_padded ++ ")" |
552 suffix <- ["ST", "ND", "RD","TH"] ]
554 to_time :: String -> Maybe UTCTime
556 listToMaybe $ catMaybes possible_parses
558 possible_parses = [ parseTime caps_time_locale fmt s |
559 fmt <- wacko_date_formats ]
561 from_time :: UTCTime -> String
563 formatTime caps_time_locale fmt t
565 upper_suffix = map toUpper (date_suffix t)
566 fmt = "%A, %B %-d" ++ upper_suffix ++ " (" ++ date_format_padded ++ ")"
570 -- | Create an 'XmlTree' containing only the given text. This is
571 -- useful for testing (un)picklers, where we don't want to have to
572 -- bother to create a dummy XML document.
576 -- >>> text_node "8:00"
577 -- NTree (XText "8:00") []
579 text_node :: String -> XmlTree
580 text_node s = NTree (XText s) []
588 -- | A list of all tests for this module. This primary exists to
589 -- eliminate the unused import/export warnings for 'unpickleDoc' and
590 -- 'text_node' which are otherwise only used in the doctests.
592 pickler_tests :: TestTree
596 [ test_pickle_of_unpickle_is_identity ]
599 -- | If we unpickle something and then pickle it, we should wind up
600 -- with the same thing we started with (plus an additional root
603 test_pickle_of_unpickle_is_identity :: TestTree
604 test_pickle_of_unpickle_is_identity =
605 testCase "pickle composed with unpickle is (almost) the identity" $ do
606 let tn = text_node "8:00"
607 let (Just utctime) = unpickleDoc xp_ambiguous_time tn
608 let actual = pickleDoc xp_ambiguous_time utctime
609 let expected = NTree (XTag (mkName "/") []) [tn]