]> gitweb.michael.orlitzky.com - dead/htsn-import.git/blob - src/TSN/Picklers.hs
Add a pickler for the weirdo EarlyLine date format.
[dead/htsn-import.git] / src / TSN / Picklers.hs
1 -- | (Un)picklers for data types present in The Sports Network XML
2 -- feed.
3 --
4 module TSN.Picklers (
5 pickler_tests,
6 xp_ambiguous_time,
7 xp_date,
8 xp_date_padded,
9 xp_datetime,
10 xp_early_line_date,
11 xp_earnings,
12 xp_gamedate,
13 xp_tba_time,
14 xp_time,
15 xp_time_dots,
16 xp_time_stamp )
17 where
18
19 -- System imports.
20 import Data.Char ( toUpper )
21 import Data.List ( intercalate )
22 import Data.List.Split ( chunksOf )
23 import Data.Maybe ( catMaybes, listToMaybe )
24 import Data.String.Utils ( replace )
25 import Data.Time.Clock ( NominalDiffTime, UTCTime, addUTCTime )
26 import Data.Time.Format ( formatTime, parseTime )
27 import Data.Tree.NTree.TypeDefs ( NTree(..) )
28 import System.Locale ( TimeLocale( wDays, months ), defaultTimeLocale )
29 import Test.Tasty ( TestTree, testGroup )
30 import Test.Tasty.HUnit ( (@?=), testCase )
31 import Text.XML.HXT.Arrow.Pickle (
32 xpText,
33 xpWrap,
34 xpWrapMaybe )
35 import Text.XML.HXT.Arrow.Pickle.Xml ( PU )
36 import Text.XML.HXT.Core (
37 XmlTree,
38 XNode( XTag, XText ),
39 mkName,
40 pickleDoc,
41 unpickleDoc )
42
43 -- Local imports.
44 import TSN.Parse (
45 parse_time_stamp,
46 time_format,
47 time_stamp_format )
48
49
50 -- | The format string for a base date in m/d/yyyy format. The
51 -- day/month are not padded at all. This will match for example,
52 --
53 -- * 2\/15\/1983
54 --
55 -- * 1\/1\/0000
56 --
57 date_format :: String
58 date_format = "%-m/%-d/%Y"
59
60
61 -- | The format string for a base date in mm/dd/yyyy format. The
62 -- day/month are padded to two characters with zeros. This will
63 -- match for example,
64 --
65 -- * 02\/15\/1983
66 --
67 -- * 01\/01\/0000
68 --
69 date_format_padded :: String
70 date_format_padded = "%0m/%0d/%Y"
71
72
73 -- | (Un)pickle a UTCTime without the time portion.
74 --
75 -- /Examples/:
76 --
77 -- This should parse:
78 --
79 -- >>> let tn = text_node "2/15/1983"
80 -- >>> unpickleDoc xp_date tn
81 -- Just 1983-02-15 00:00:00 UTC
82 --
83 -- But for some reason, it can also parse a leading zero in the
84 -- month. Whatever. This isn't required behavior.
85 --
86 -- >>> let tn = text_node "02/15/1983"
87 -- >>> unpickleDoc xp_date tn
88 -- Just 1983-02-15 00:00:00 UTC
89 --
90 xp_date :: PU UTCTime
91 xp_date =
92 (to_date, from_date) `xpWrapMaybe` xpText
93 where
94 to_date :: String -> Maybe UTCTime
95 to_date = parseTime defaultTimeLocale date_format
96
97 from_date :: UTCTime -> String
98 from_date = formatTime defaultTimeLocale date_format
99
100
101 -- | (Un)pickle a UTCTime without the time portion. The day/month are
102 -- padded to two characters with zeros.
103 --
104 -- Examples:
105 --
106 -- >>> let tn = text_node "02/15/1983"
107 -- >>> unpickleDoc xp_date_padded tn
108 -- Just 1983-02-15 00:00:00 UTC
109 --
110 xp_date_padded :: PU UTCTime
111 xp_date_padded =
112 (to_date, from_date) `xpWrapMaybe` xpText
113 where
114 to_date :: String -> Maybe UTCTime
115 to_date = parseTime defaultTimeLocale date_format_padded
116
117 from_date :: UTCTime -> String
118 from_date = formatTime defaultTimeLocale date_format_padded
119
120
121
122 -- | Format a number as a string using a comma as the thousands
123 -- separator.
124 --
125 -- Examples:
126 --
127 -- >>> format_commas 0
128 -- "0"
129 -- >>> format_commas 10
130 -- "10"
131 -- >>> format_commas 100
132 -- "100"
133 -- >>> format_commas 1000
134 -- "1,000"
135 -- >>> format_commas 10000
136 -- "10,000"
137 -- >>> format_commas 100000
138 -- "100,000"
139 -- >>> format_commas 1000000
140 -- "1,000,000"
141 --
142 format_commas :: Int -> String
143 format_commas x =
144 reverse (intercalate "," $ chunksOf 3 $ reverse $ show x)
145
146
147 -- | Parse \<Earnings\> from an 'AutoRaceResultsListing'. These are
148 -- essentially 'Int's, but they look like,
149 --
150 -- * \<Earnings\>336,826\</Earnings\>
151 --
152 -- * \<Earnings\>1,000,191\</Earnings\>
153 --
154 -- * \<Earnings\>TBA\</Earnings\>
155 --
156 -- Examples:
157 --
158 -- >>> let tn = text_node "1,000,191"
159 -- >>> unpickleDoc xp_earnings tn
160 -- Just (Just 1000191)
161 --
162 -- >>> let tn = text_node "TBA"
163 -- >>> unpickleDoc xp_earnings tn
164 -- Just Nothing
165 --
166 xp_earnings :: PU (Maybe Int)
167 xp_earnings =
168 (to_earnings, from_earnings) `xpWrap` xpText
169 where
170 strip_commas :: String -> String
171 strip_commas = replace "," ""
172
173 to_earnings :: String -> Maybe Int
174 to_earnings s
175 | s == "TBA" = Nothing
176 | otherwise = Just $ (read . strip_commas) s
177
178 from_earnings :: Maybe Int -> String
179 from_earnings Nothing = "TBA"
180 from_earnings (Just i) = format_commas i
181
182
183
184 -- | (Un)pickle an unpadded 'UTCTime'. Used for example on the
185 -- \<RaceDate\> elements in an 'AutoRaceResults' message.
186 --
187 -- Examples:
188 --
189 -- >>> let tn = text_node "6/1/2014 1:00:00 PM"
190 -- >>> unpickleDoc xp_datetime tn
191 -- Just 2014-06-01 13:00:00 UTC
192 --
193 -- >>> let tn = text_node "5/24/2014 2:45:00 PM"
194 -- >>> unpickleDoc xp_datetime tn
195 -- Just 2014-05-24 14:45:00 UTC
196 --
197 -- Padded! For some reason it works with only one zero in front. I
198 -- dunno man. NOT required (or even desired?) behavior.
199 --
200 -- >>> let tn = text_node "05/24/2014 2:45:00 PM"
201 -- >>> unpickleDoc xp_datetime tn
202 -- Just 2014-05-24 14:45:00 UTC
203 --
204 xp_datetime :: PU UTCTime
205 xp_datetime =
206 (to_datetime, from_datetime) `xpWrapMaybe` xpText
207 where
208 format = date_format ++ " " ++ "%-I:%M:%S %p"
209
210 to_datetime :: String -> Maybe UTCTime
211 to_datetime = parseTime defaultTimeLocale format
212
213 from_datetime :: UTCTime -> String
214 from_datetime = formatTime defaultTimeLocale format
215
216
217
218 -- | Takes a 'UTCTime', and returns the English suffix that would be
219 -- appropriate after the day of the month. For example, if we have a
220 -- UTCTime representing Christmas, this would return \"th\" because
221 -- \"th\" is the right suffix of \"December 25th\".
222 --
223 -- Examples:
224 --
225 -- >>> :{
226 -- let parse_date :: String -> Maybe UTCTime;
227 -- parse_date = parseTime defaultTimeLocale date_format;
228 -- :}
229 --
230 -- >>> let (Just d1) = parse_date "1/1/1970"
231 -- >>> date_suffix d1
232 -- "st"
233 --
234 -- >>> let (Just d2) = parse_date "1/2/1970"
235 -- >>> date_suffix d2
236 -- "nd"
237 --
238 -- >>> let (Just d3) = parse_date "1/3/1970"
239 -- >>> date_suffix d3
240 -- "rd"
241 --
242 -- >>> let (Just d4) = parse_date "1/4/1970"
243 -- >>> date_suffix d4
244 -- "th"
245 --
246 date_suffix :: UTCTime -> String
247 date_suffix t =
248 case (reverse daystr) of
249 [] -> []
250 ('1':_) -> "st"
251 ('2':_) -> "nd"
252 ('3':_) -> "rd"
253 _ -> "th"
254 where
255 daystr = formatTime defaultTimeLocale "%d" t
256
257
258 -- | (Un)pickle a UTCTime from a weather forecast's gamedate. Example
259 -- input looks like,
260 --
261 -- When unpickling we get rid of the suffixes \"st\", \"nd\", \"rd\", and
262 -- \"th\". During pickling, we add them back based on the last digit
263 -- of the date.
264 --
265 -- Examples:
266 --
267 -- >>> let tn = text_node "Monday, December 30th"
268 -- >>> let (Just gd) = unpickleDoc xp_gamedate tn
269 -- >>> gd
270 -- 1970-12-30 00:00:00 UTC
271 -- >>> pickleDoc xp_gamedate gd
272 -- NTree (XTag "/" []) [NTree (XText "Wednesday, December 30th") []]
273 --
274 xp_gamedate :: PU UTCTime
275 xp_gamedate =
276 (to_gamedate, from_gamedate) `xpWrapMaybe` xpText
277 where
278 format = "%A, %B %-d"
279
280 to_gamedate :: String -> Maybe UTCTime
281 to_gamedate s =
282 parseTime defaultTimeLocale format s'
283 where
284 s' = case (reverse s) of
285 (c2:c1:cs) -> let suffix = [c1,c2]
286 in
287 if suffix `elem` ["st","nd","rd","th"]
288 then reverse cs
289 else s -- Unknown suffix, leave it alone.
290
291 _ -> s -- The String is less than two characters long,
292 -- leave it alone.
293
294
295 from_gamedate :: UTCTime -> String
296 from_gamedate d = s ++ (date_suffix d)
297 where
298 s = formatTime defaultTimeLocale format d
299
300
301
302
303
304
305
306 -- | (Un)pickle a UTCTime without the date portion. Doesn't work if
307 -- the fields aren't zero-padded to two characters.
308 --
309 -- /Examples/:
310 --
311 -- Padded, should work:
312 --
313 -- >>> let tn = text_node "04:35 PM"
314 -- >>> unpickleDoc xp_time tn
315 -- Just 1970-01-01 16:35:00 UTC
316 --
317 -- Unpadded, should fail:
318 --
319 -- >>> let tn = text_node "4:35 PM"
320 -- >>> unpickleDoc xp_time tn
321 -- Nothing
322 --
323 xp_time :: PU UTCTime
324 xp_time =
325 (to_time, from_time) `xpWrapMaybe` xpText
326 where
327 to_time :: String -> Maybe UTCTime
328 to_time = parseTime defaultTimeLocale time_format
329
330 from_time :: UTCTime -> String
331 from_time = formatTime defaultTimeLocale time_format
332
333
334 -- | (Un)pickle a UTCTime without the date portion. This differs from
335 -- 'xp_time' in that it uses periods in the AM/PM part, i.e. \"A.M.\"
336 -- and \"P.M.\" It also doesn't use padding for the \"hours\" part.
337 --
338 -- /Examples/:
339 --
340 -- A standard example of the correct form:
341 --
342 -- >>> let tn = text_node "11:30 A.M."
343 -- >>> let (Just result) = unpickleDoc xp_time_dots tn
344 -- >>> result
345 -- 1970-01-01 11:30:00 UTC
346 -- >>> pickleDoc xp_time_dots result
347 -- NTree (XTag "/" []) [NTree (XText "11:30 A.M.") []]
348 --
349 -- Another miracle, it still parses with a leading zero!
350 --
351 -- >>> let tn = text_node "01:30 A.M."
352 -- >>> unpickleDoc xp_time_dots tn
353 -- Just 1970-01-01 01:30:00 UTC
354 --
355 xp_time_dots :: PU UTCTime
356 xp_time_dots =
357 (to_time, from_time) `xpWrapMaybe` xpText
358 where
359 -- | The hours arent padded with zeros.
360 nopad_time_format :: String
361 nopad_time_format = "%-I:%M %p"
362
363 to_time :: String -> Maybe UTCTime
364 to_time = (parseTime defaultTimeLocale nopad_time_format) . (replace "." "")
365
366 from_time :: UTCTime -> String
367 from_time t =
368 replace "AM" "A.M." (replace "PM" "P.M." s)
369 where
370 s = formatTime defaultTimeLocale nopad_time_format t
371
372
373 -- | (Un)pickle a UTCTime without the date portion, allowing for a
374 -- value of \"TBA\" (which gets translated to 'Nothing').
375 --
376 -- /Examples/:
377 --
378 -- A failed parse will return 'Nothing':
379 --
380 -- >>> let tn = text_node "YO"
381 -- >>> unpickleDoc xp_tba_time tn
382 -- Just Nothing
383 --
384 -- And so will parsing a \"TBA\":
385 --
386 -- >>> let tn = text_node "TBA"
387 -- >>> unpickleDoc xp_tba_time tn
388 -- Just Nothing
389 --
390 -- But re-pickling 'Nothing' gives only \"TBA\":
391 --
392 -- >>> pickleDoc xp_tba_time Nothing
393 -- NTree (XTag "/" []) [NTree (XText "TBA") []]
394 --
395 -- A normal time is also parsed successfully, of course:
396 --
397 -- >>> let tn = text_node "08:10 PM"
398 -- >>> unpickleDoc xp_tba_time tn
399 -- Just (Just 1970-01-01 20:10:00 UTC)
400 --
401 xp_tba_time :: PU (Maybe UTCTime)
402 xp_tba_time =
403 (to_time, from_time) `xpWrap` xpText
404 where
405 to_time :: String -> Maybe UTCTime
406 to_time s
407 | s == "TBA" = Nothing
408 | otherwise = parseTime defaultTimeLocale time_format s
409
410 from_time :: Maybe UTCTime -> String
411 from_time Nothing = "TBA"
412 from_time (Just t) = formatTime defaultTimeLocale time_format t
413
414
415
416 -- | (Un)pickle the \<time_stamp\> element format to/from a 'UTCTime'.
417 -- The time_stamp elements look something like,
418 --
419 -- \<time_stamp\> January 6, 2014, at 10:11 PM ET \</time_stamp\>
420 --
421 -- TSN doesn't provide a proper time zone name, so we assume that
422 -- it's always Eastern Standard Time. EST is UTC-5, so we
423 -- add/subtract 5 hours to convert to/from UTC.
424 --
425 -- Examples:
426 --
427 -- >>> let tn = text_node " January 6, 2014, at 10:11 PM ET "
428 -- >>> unpickleDoc xp_time_stamp tn
429 -- Just 2014-01-07 03:11:00 UTC
430 --
431 xp_time_stamp :: PU UTCTime
432 xp_time_stamp =
433 (parse_time_stamp, from_time_stamp) `xpWrapMaybe` xpText
434 where
435 five_hours :: NominalDiffTime
436 five_hours = 5 * 60 * 60
437
438 subtract_five :: UTCTime -> UTCTime
439 subtract_five = addUTCTime (-1 * five_hours)
440
441 from_time_stamp :: UTCTime -> String
442 from_time_stamp =
443 formatTime defaultTimeLocale time_stamp_format . subtract_five
444
445
446 -- | (Un)pickle an ambiguous 12-hour AM/PM time, which is ambiguous
447 -- because it's missing the AM/PM part.
448 --
449 -- Examples:
450 --
451 -- >>> let tn = text_node "8:00"
452 -- >>> unpickleDoc xp_ambiguous_time tn
453 -- Just 1970-01-01 08:00:00 UTC
454 --
455 xp_ambiguous_time :: PU UTCTime
456 xp_ambiguous_time =
457 (to_time, from_time) `xpWrapMaybe` xpText
458 where
459 ambiguous_time_format :: String
460 ambiguous_time_format = "%-I:%M"
461
462 to_time :: String -> Maybe UTCTime
463 to_time = parseTime defaultTimeLocale ambiguous_time_format
464
465 from_time :: UTCTime -> String
466 from_time =
467 formatTime defaultTimeLocale ambiguous_time_format
468
469
470 -- | Pickle a date value from a \<date\> element as they appear in the
471 -- early lines. This is a particularly wacky format, but then so is
472 -- the associated time (see 'xp_ambiguous_time').
473 --
474 -- Examples:
475 --
476 -- >>> let tn = text_node "SUNDAY, MAY 25TH (05/25/2014)"
477 -- >>> let (Just result) = unpickleDoc xp_early_line_date tn
478 -- >>> result
479 -- 2014-05-25 00:00:00 UTC
480 -- >>> pickleDoc xp_early_line_date result
481 -- NTree (XTag "/" []) [NTree (XText "SUNDAY, MAY 25TH (05/25/2014)") []]
482 --
483 xp_early_line_date :: PU UTCTime
484 xp_early_line_date =
485 (to_time, from_time) `xpWrapMaybe` xpText
486 where
487 -- | We need to create our own time locale that talks IN ALL CAPS.
488 -- Actually, 'parseTime' doesn't seem to care about the
489 -- case. But when we spit it back out again ('formatTime'),
490 -- we'll want it to be in all caps.
491 --
492 caps_time_locale :: TimeLocale
493 caps_time_locale =
494 defaultTimeLocale { wDays = caps_days, months = caps_months }
495
496 caps_days :: [(String,String)]
497 caps_days = map both_to_upper (wDays defaultTimeLocale)
498
499 caps_months :: [(String,String)]
500 caps_months = map both_to_upper (months defaultTimeLocale)
501
502 both_to_upper :: (String,String) -> (String,String)
503 both_to_upper (s1,s2) = (map toUpper s1, map toUpper s2)
504
505 wacko_date_formats :: [String]
506 wacko_date_formats =
507 ["%A, %B %d" ++ suffix ++ " (" ++ date_format_padded ++ ")" |
508 suffix <- ["ST", "ND", "RD","TH"] ]
509
510 to_time :: String -> Maybe UTCTime
511 to_time s =
512 listToMaybe $ catMaybes possible_parses
513 where
514 possible_parses = [ parseTime caps_time_locale fmt s |
515 fmt <- wacko_date_formats ]
516
517 from_time :: UTCTime -> String
518 from_time t =
519 formatTime caps_time_locale fmt t
520 where
521 upper_suffix = map toUpper (date_suffix t)
522 fmt = "%A, %B %d" ++ upper_suffix ++ " (" ++ date_format_padded ++ ")"
523
524 -- | Create an 'XmlTree' containing only the given text. This is
525 -- useful for testing (un)picklers, where we don't want to have to
526 -- bother to create a dummy XML document.
527 --
528 -- Examples:
529 --
530 -- >>> text_node "8:00"
531 -- NTree (XText "8:00") []
532 --
533 text_node :: String -> XmlTree
534 text_node s = NTree (XText s) []
535
536
537
538 --
539 -- * Tasty Tests
540 --
541
542 -- | A list of all tests for this module. This primary exists to
543 -- eliminate the unused import/export warnings for 'unpickleDoc' and
544 -- 'text_node' which are otherwise only used in the doctests.
545 --
546 pickler_tests :: TestTree
547 pickler_tests =
548 testGroup
549 "Pickler tests"
550 [ test_pickle_of_unpickle_is_identity ]
551
552
553 -- | If we unpickle something and then pickle it, we should wind up
554 -- with the same thing we started with (plus an additional root
555 -- element).
556 --
557 test_pickle_of_unpickle_is_identity :: TestTree
558 test_pickle_of_unpickle_is_identity =
559 testCase "pickle composed with unpickle is (almost) the identity" $ do
560 let tn = text_node "8:00"
561 let (Just utctime) = unpickleDoc xp_ambiguous_time tn
562 let actual = pickleDoc xp_ambiguous_time utctime
563 let expected = NTree (XTag (mkName "/") []) [tn]
564 actual @?= expected