1 {-# LANGUAGE ScopedTypeVariables #-}
3 -- | (Un)picklers for data types present in The Sports Network XML
15 xp_fracpart_only_double,
25 import Data.Char ( toUpper )
26 import Data.List ( intercalate )
27 import Data.List.Split ( chunksOf )
28 import Data.Maybe ( catMaybes, listToMaybe )
29 import Data.String.Utils ( replace )
30 import Data.Time.Clock ( UTCTime )
31 import Data.Time.Format ( formatTime, parseTime )
32 import Data.Tree.NTree.TypeDefs ( NTree(..) )
33 import System.Locale ( TimeLocale( wDays, months ), defaultTimeLocale )
34 import Test.Tasty ( TestTree, testGroup )
35 import Test.Tasty.HUnit ( (@?=), testCase )
36 import Text.Read ( readMaybe )
37 import Text.XML.HXT.Arrow.Pickle (
41 import Text.XML.HXT.Arrow.Pickle.Xml ( PU )
42 import Text.XML.HXT.Core (
56 -- | The format string for a base date in m/d/yyyy format. The
57 -- day/month are not padded at all. This will match for example,
64 date_format = "%-m/%-d/%Y"
67 -- | The format string for a base date in mm/dd/yyyy format. The
68 -- day/month are padded to two characters with zeros. This will
75 date_format_padded :: String
76 date_format_padded = "%0m/%0d/%Y"
79 -- | (Un)pickle a UTCTime without the time portion.
85 -- >>> let tn = text_node "2/15/1983"
86 -- >>> unpickleDoc xp_date tn
87 -- Just 1983-02-15 00:00:00 UTC
89 -- But for some reason, it can also parse a leading zero in the
90 -- month. Whatever. This isn't required behavior.
92 -- >>> let tn = text_node "02/15/1983"
93 -- >>> unpickleDoc xp_date tn
94 -- Just 1983-02-15 00:00:00 UTC
98 (to_date, from_date) `xpWrapMaybe` xpText
100 to_date :: String -> Maybe UTCTime
101 to_date = parseTime defaultTimeLocale date_format
103 from_date :: UTCTime -> String
104 from_date = formatTime defaultTimeLocale date_format
107 -- | (Un)pickle a UTCTime without the time portion. The day/month are
108 -- padded to two characters with zeros.
112 -- >>> let tn = text_node "02/15/1983"
113 -- >>> unpickleDoc xp_date_padded tn
114 -- Just 1983-02-15 00:00:00 UTC
116 -- >>> let tn = text_node "06/07/2014"
117 -- >>> unpickleDoc xp_date_padded tn
118 -- Just 2014-06-07 00:00:00 UTC
120 xp_date_padded :: PU UTCTime
122 (to_date, from_date) `xpWrapMaybe` xpText
124 to_date :: String -> Maybe UTCTime
125 to_date = parseTime defaultTimeLocale date_format_padded
127 from_date :: UTCTime -> String
128 from_date = formatTime defaultTimeLocale date_format_padded
132 -- | Format a number as a string using a comma as the thousands
137 -- >>> format_commas 0
139 -- >>> format_commas 10
141 -- >>> format_commas 100
143 -- >>> format_commas 1000
145 -- >>> format_commas 10000
147 -- >>> format_commas 100000
149 -- >>> format_commas 1000000
152 format_commas :: Int -> String
154 reverse (intercalate "," $ chunksOf 3 $ reverse $ show x)
158 -- | Parse \<Earnings\> from an 'AutoRaceResultsListing'. These are
159 -- essentially 'Int's, but they look like,
161 -- * \<Earnings\>336,826\</Earnings\>
163 -- * \<Earnings\>1,000,191\</Earnings\>
165 -- * \<Earnings\>TBA\</Earnings\>
169 -- >>> let tn = text_node "1,000,191"
170 -- >>> unpickleDoc xp_earnings tn
171 -- Just (Just 1000191)
173 -- >>> let tn = text_node "TBA"
174 -- >>> unpickleDoc xp_earnings tn
177 xp_earnings :: PU (Maybe Int)
179 (to_earnings, from_earnings) `xpWrap` xpText
181 strip_commas :: String -> String
182 strip_commas = replace "," ""
184 to_earnings :: String -> Maybe Int
186 | s == "TBA" = Nothing
187 | otherwise = Just $ (read . strip_commas) s
189 from_earnings :: Maybe Int -> String
190 from_earnings Nothing = "TBA"
191 from_earnings (Just i) = format_commas i
195 -- | Pickle a 'Double' that can be missing its leading zero (for
196 -- values less than one). For example, we've seen,
198 -- <TrackLength KPH=".805">0.5</TrackLength>
200 -- Which 'xpPrim' can't handle without the leading
201 -- zero. Unfortunately there's no way pickle/unpickle can be
202 -- inverses of each other here, since \"0.5\" and \".5\" should
203 -- unpickle to the same 'Double'.
207 -- >>> let tn = text_node "0.5"
208 -- >>> unpickleDoc xp_fracpart_only_double tn
211 -- >>> let tn = text_node ".5"
212 -- >>> unpickleDoc xp_fracpart_only_double tn
215 -- >>> let tn = text_node "foo"
216 -- >>> unpickleDoc xp_fracpart_only_double tn
219 xp_fracpart_only_double :: PU Double
220 xp_fracpart_only_double =
221 (to_double, from_double) `xpWrapMaybe` xpText
223 -- | Convert a 'String' to a 'Double', maybe. We always prepend a
224 -- zero, since it will fix the fraction-only values, and not hurt
225 -- the ones that already have a leading integer.
226 to_double :: String -> Maybe Double
227 to_double s = readMaybe ("0" ++ s)
229 from_double :: Double -> String
234 -- | (Un)pickle an unpadded 'UTCTime'. Used for example on the
235 -- \<RaceDate\> elements in an 'AutoRaceResults' message.
239 -- >>> let tn = text_node "6/1/2014 1:00:00 PM"
240 -- >>> unpickleDoc xp_datetime tn
241 -- Just 2014-06-01 13:00:00 UTC
243 -- >>> let tn = text_node "5/24/2014 2:45:00 PM"
244 -- >>> unpickleDoc xp_datetime tn
245 -- Just 2014-05-24 14:45:00 UTC
247 -- Padded! For some reason it works with only one zero in front. I
248 -- dunno man. NOT required (or even desired?) behavior.
250 -- >>> let tn = text_node "05/24/2014 2:45:00 PM"
251 -- >>> unpickleDoc xp_datetime tn
252 -- Just 2014-05-24 14:45:00 UTC
254 xp_datetime :: PU UTCTime
256 (to_datetime, from_datetime) `xpWrapMaybe` xpText
258 format = date_format ++ " " ++ "%-I:%M:%S %p"
260 to_datetime :: String -> Maybe UTCTime
261 to_datetime = parseTime defaultTimeLocale format
263 from_datetime :: UTCTime -> String
264 from_datetime = formatTime defaultTimeLocale format
268 -- | Takes a 'UTCTime', and returns the English suffix that would be
269 -- appropriate after the day of the month. For example, if we have a
270 -- UTCTime representing Christmas, this would return \"th\" because
271 -- \"th\" is the right suffix of \"December 25th\".
275 -- >>> import Data.Maybe ( fromJust )
277 -- let parse_date :: String -> Maybe UTCTime
278 -- parse_date = parseTime defaultTimeLocale date_format
281 -- >>> let dates = [ "1/" ++ (d : "/1970") | d <- ['1'..'9'] ]
282 -- >>> let suffixes = map (date_suffix . fromJust . parse_date) dates
284 -- ["st","nd","rd","th","th","th","th","th","th"]
286 date_suffix :: UTCTime -> String
288 case (reverse daystr) of
295 daystr = formatTime defaultTimeLocale "%d" t
298 -- | (Un)pickle a UTCTime from a weather forecast's gamedate. Example
301 -- When unpickling we get rid of the suffixes \"st\", \"nd\", \"rd\", and
302 -- \"th\". During pickling, we add them back based on the last digit
307 -- >>> let tn = text_node "Monday, December 30th"
308 -- >>> let (Just gd) = unpickleDoc xp_gamedate tn
310 -- 1970-12-30 00:00:00 UTC
311 -- >>> pickleDoc xp_gamedate gd
312 -- NTree (XTag "/" []) [NTree (XText "Wednesday, December 30th") []]
314 xp_gamedate :: PU UTCTime
316 (to_gamedate, from_gamedate) `xpWrapMaybe` xpText
318 format = "%A, %B %-d"
320 to_gamedate :: String -> Maybe UTCTime
322 parseTime defaultTimeLocale format s'
324 s' = case (reverse s) of
325 (c2:c1:cs) -> let suffix = [c1,c2]
327 if suffix `elem` ["st","nd","rd","th"]
329 else s -- Unknown suffix, leave it alone.
331 _ -> s -- The String is less than two characters long,
335 from_gamedate :: UTCTime -> String
336 from_gamedate d = s ++ (date_suffix d)
338 s = formatTime defaultTimeLocale format d
346 -- | (Un)pickle a UTCTime without the date portion. Doesn't work if
347 -- the fields aren't zero-padded to two characters.
351 -- Padded, should work:
353 -- >>> let tn = text_node "04:35 PM"
354 -- >>> unpickleDoc xp_time tn
355 -- Just 1970-01-01 16:35:00 UTC
357 -- Unpadded, should fail:
359 -- >>> let tn = text_node "4:35 PM"
360 -- >>> unpickleDoc xp_time tn
363 xp_time :: PU UTCTime
365 (to_time, from_time) `xpWrapMaybe` xpText
367 to_time :: String -> Maybe UTCTime
368 to_time = parseTime defaultTimeLocale time_format
370 from_time :: UTCTime -> String
371 from_time = formatTime defaultTimeLocale time_format
374 -- | (Un)pickle a UTCTime without the date portion. This differs from
375 -- 'xp_time' in that it uses periods in the AM/PM part, i.e. \"A.M.\"
376 -- and \"P.M.\" It also doesn't use padding for the \"hours\" part.
380 -- A standard example of the correct form:
382 -- >>> let tn = text_node "11:30 A.M."
383 -- >>> let (Just result) = unpickleDoc xp_time_dots tn
385 -- 1970-01-01 11:30:00 UTC
386 -- >>> pickleDoc xp_time_dots result
387 -- NTree (XTag "/" []) [NTree (XText "11:30 A.M.") []]
389 -- Another miracle, it still parses with a leading zero!
391 -- >>> let tn = text_node "01:30 A.M."
392 -- >>> unpickleDoc xp_time_dots tn
393 -- Just 1970-01-01 01:30:00 UTC
395 xp_time_dots :: PU UTCTime
397 (to_time, from_time) `xpWrapMaybe` xpText
399 -- | The hours arent padded with zeros.
400 nopad_time_format :: String
401 nopad_time_format = "%-I:%M %p"
403 to_time :: String -> Maybe UTCTime
404 to_time = (parseTime defaultTimeLocale nopad_time_format) . (replace "." "")
406 from_time :: UTCTime -> String
408 replace "AM" "A.M." (replace "PM" "P.M." s)
410 s = formatTime defaultTimeLocale nopad_time_format t
413 -- | (Un)pickle a UTCTime without the date portion, allowing for a
414 -- value of \"TBA\" (which gets translated to 'Nothing').
418 -- A failed parse will return 'Nothing':
420 -- >>> let tn = text_node "YO"
421 -- >>> unpickleDoc xp_tba_time tn
424 -- And so will parsing a \"TBA\":
426 -- >>> let tn = text_node "TBA"
427 -- >>> unpickleDoc xp_tba_time tn
430 -- But re-pickling 'Nothing' gives only \"TBA\":
432 -- >>> pickleDoc xp_tba_time Nothing
433 -- NTree (XTag "/" []) [NTree (XText "TBA") []]
435 -- A normal time is also parsed successfully, of course:
437 -- >>> let tn = text_node "08:10 PM"
438 -- >>> unpickleDoc xp_tba_time tn
439 -- Just (Just 1970-01-01 20:10:00 UTC)
441 xp_tba_time :: PU (Maybe UTCTime)
443 (to_time, from_time) `xpWrap` xpText
445 to_time :: String -> Maybe UTCTime
447 | s == "TBA" = Nothing
448 | otherwise = parseTime defaultTimeLocale time_format s
450 from_time :: Maybe UTCTime -> String
451 from_time Nothing = "TBA"
452 from_time (Just t) = formatTime defaultTimeLocale time_format t
455 -- | (Un)pickle a 'Int', allowing for a value of \"TBA\" (which gets
456 -- translated to 'Nothing').
460 -- A failed parse will return 'Nothing':
462 -- >>> let tn = text_node "YO"
463 -- >>> unpickleDoc xp_tba_int tn
466 -- And so will parsing a \"TBA\":
468 -- >>> let tn = text_node "TBA"
469 -- >>> unpickleDoc xp_tba_int tn
472 -- But re-pickling 'Nothing' gives only \"TBA\":
474 -- >>> pickleDoc xp_tba_int Nothing
475 -- NTree (XTag "/" []) [NTree (XText "TBA") []]
477 -- A normal integer is also parsed successfully, of course:
479 -- >>> let tn = text_node "110"
480 -- >>> unpickleDoc xp_tba_int tn
483 xp_tba_int :: PU (Maybe Int)
485 (to_int, from_int) `xpWrap` xpText
487 to_int :: String -> Maybe Int
490 from_int :: Maybe Int -> String
491 from_int Nothing = "TBA"
492 from_int (Just t) = show t
496 -- | (Un)pickle the \<time_stamp\> element format to/from a 'UTCTime'.
497 -- The time_stamp elements look something like,
499 -- \<time_stamp\> January 6, 2014, at 10:11 PM ET \</time_stamp\>
501 -- TSN doesn't provide a proper time zone name, only \"ET\" for
502 -- \"Eastern Time\". But \"Eastern Time\" changes throughout the
503 -- year, depending on one's location, for daylight-savings
504 -- time. It's really not any more useful to be off by one hour than
505 -- it is to be off by 5 hours, so rather than guess at EDT/EST, we
506 -- just store the timestamp as UTC.
510 -- >>> let tn = text_node " January 6, 2014, at 10:11 PM ET "
511 -- >>> let (Just tstamp) = unpickleDoc xp_time_stamp tn
513 -- 2014-01-06 22:11:00 UTC
514 -- >>> pickleDoc xp_time_stamp tstamp
515 -- NTree (XTag "/" []) [NTree (XText " January 6, 2014, at 10:11 PM ET ") []]
517 xp_time_stamp :: PU UTCTime
519 (parse_time_stamp, from_time_stamp) `xpWrapMaybe` xpText
521 -- | We have to re-pad the time_stamp_format with a leading and
522 -- trailing space; see the documentation of 'time_stamp_format'
523 -- for more information.
524 from_time_stamp :: UTCTime -> String
526 formatTime defaultTimeLocale (" " ++ time_stamp_format ++ " ")
530 -- | (Un)pickle an ambiguous 12-hour AM/PM time, which is ambiguous
531 -- because it's missing the AM/PM part.
535 -- >>> let tn = text_node "8:00"
536 -- >>> unpickleDoc xp_ambiguous_time tn
537 -- Just 1970-01-01 08:00:00 UTC
539 xp_ambiguous_time :: PU UTCTime
541 (to_time, from_time) `xpWrapMaybe` xpText
543 ambiguous_time_format :: String
544 ambiguous_time_format = "%-I:%M"
546 to_time :: String -> Maybe UTCTime
547 to_time = parseTime defaultTimeLocale ambiguous_time_format
549 from_time :: UTCTime -> String
551 formatTime defaultTimeLocale ambiguous_time_format
554 -- | Pickle a date value from a \<date\> element as they appear in the
555 -- early lines. This is a particularly wacky format, but then so is
556 -- the associated time (see 'xp_ambiguous_time').
560 -- >>> let tn = text_node "SUNDAY, MAY 25TH (05/25/2014)"
561 -- >>> let (Just result) = unpickleDoc xp_early_line_date tn
563 -- 2014-05-25 00:00:00 UTC
564 -- >>> pickleDoc xp_early_line_date result
565 -- NTree (XTag "/" []) [NTree (XText "SUNDAY, MAY 25TH (05/25/2014)") []]
567 -- >>> let tn = text_node "SATURDAY, JUNE 7TH (06/07/2014)"
568 -- >>> let (Just result) = unpickleDoc xp_early_line_date tn
570 -- 2014-06-07 00:00:00 UTC
571 -- >>> pickleDoc xp_early_line_date result
572 -- NTree (XTag "/" []) [NTree (XText "SATURDAY, JUNE 7TH (06/07/2014)") []]
574 xp_early_line_date :: PU UTCTime
576 (to_time, from_time) `xpWrapMaybe` xpText
578 -- | We need to create our own time locale that talks IN ALL CAPS.
579 -- Actually, 'parseTime' doesn't seem to care about the
580 -- case. But when we spit it back out again ('formatTime'),
581 -- we'll want it to be in all caps.
583 caps_time_locale :: TimeLocale
585 defaultTimeLocale { wDays = caps_days, months = caps_months }
587 caps_days :: [(String,String)]
588 caps_days = map both_to_upper (wDays defaultTimeLocale)
590 caps_months :: [(String,String)]
591 caps_months = map both_to_upper (months defaultTimeLocale)
593 both_to_upper :: (String,String) -> (String,String)
594 both_to_upper (s1,s2) = (map toUpper s1, map toUpper s2)
596 wacko_date_formats :: [String]
598 ["%A, %B %-d" ++ suffix ++ " (" ++ date_format_padded ++ ")" |
599 suffix <- ["ST", "ND", "RD","TH"] ]
601 to_time :: String -> Maybe UTCTime
603 listToMaybe $ catMaybes possible_parses
605 possible_parses = [ parseTime caps_time_locale fmt s |
606 fmt <- wacko_date_formats ]
608 from_time :: UTCTime -> String
610 formatTime caps_time_locale fmt t
612 upper_suffix = map toUpper (date_suffix t)
613 fmt = "%A, %B %-d" ++ upper_suffix ++ " (" ++ date_format_padded ++ ")"
616 -- | This is a replacement for @xpOption xpFoo@ within an 'xpAttr'.
617 -- There's a bug <https://github.com/UweSchmidt/hxt/issues/39> in
618 -- newer versions of HXT that prevents us from using the usual
619 -- 'xpOption' solution, so this is our stopgap. It should work on
620 -- any type that can be unpickled with a plain read/show.
622 xp_attr_option :: forall a. (Read a, Show a) => PU (Maybe a)
624 (to_a, from_a) `xpWrap` xpText
626 to_a :: String -> Maybe a
629 from_a :: Maybe a -> String
631 from_a (Just x) = show x
634 -- | Create an 'XmlTree' containing only the given text. This is
635 -- useful for testing (un)picklers, where we don't want to have to
636 -- bother to create a dummy XML document.
640 -- >>> text_node "8:00"
641 -- NTree (XText "8:00") []
643 text_node :: String -> XmlTree
644 text_node s = NTree (XText s) []
652 -- | A list of all tests for this module. This primary exists to
653 -- eliminate the unused import/export warnings for 'unpickleDoc' and
654 -- 'text_node' which are otherwise only used in the doctests.
656 pickler_tests :: TestTree
660 [ test_pickle_of_unpickle_is_identity ]
663 -- | If we unpickle something and then pickle it, we should wind up
664 -- with the same thing we started with (plus an additional root
667 test_pickle_of_unpickle_is_identity :: TestTree
668 test_pickle_of_unpickle_is_identity =
669 testCase "pickle composed with unpickle is (almost) the identity" $ do
670 let tn = text_node "8:00"
671 let (Just utctime) = unpickleDoc xp_ambiguous_time tn
672 let actual = pickleDoc xp_ambiguous_time utctime
673 let expected = NTree (XTag (mkName "/") []) [tn]