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.
74 (to_date, from_date) `xpWrapMaybe` xpText
76 to_date :: String -> Maybe UTCTime
77 to_date = parseTime defaultTimeLocale date_format
79 from_date :: UTCTime -> String
80 from_date = formatTime defaultTimeLocale date_format
83 -- | (Un)pickle a UTCTime without the time portion. The day/month are
84 -- padded to two characters with zeros.
86 xp_date_padded :: PU UTCTime
88 (to_date, from_date) `xpWrapMaybe` xpText
90 to_date :: String -> Maybe UTCTime
91 to_date = parseTime defaultTimeLocale date_format_padded
93 from_date :: UTCTime -> String
94 from_date = formatTime defaultTimeLocale date_format_padded
98 -- | Format a number as a string using a comma as the thousands
103 -- >>> format_commas 0
105 -- >>> format_commas 10
107 -- >>> format_commas 100
109 -- >>> format_commas 1000
111 -- >>> format_commas 10000
113 -- >>> format_commas 100000
115 -- >>> format_commas 1000000
118 format_commas :: Int -> String
120 reverse (intercalate "," $ chunksOf 3 $ reverse $ show x)
122 -- | Parse \<Earnings\> from an 'AutoRaceResultsListing'. These are
123 -- essentially 'Int's, but they look like,
125 -- * \<Earnings\>336,826\</Earnings\>
127 -- * \<Earnings\>1,000,191\</Earnings\>
129 -- * \<Earnings\>TBA\</Earnings\>
131 xp_earnings :: PU (Maybe Int)
133 (to_earnings, from_earnings) `xpWrap` xpText
135 strip_commas :: String -> String
136 strip_commas = replace "," ""
138 to_earnings :: String -> Maybe Int
140 | s == "TBA" = Nothing
141 | otherwise = Just $ (read . strip_commas) s
143 from_earnings :: Maybe Int -> String
144 from_earnings Nothing = "TBA"
145 from_earnings (Just i) = format_commas i
148 -- | (Un)pickle an unpadded 'UTCTime'. Used for example on the
149 -- \<RaceDate\> elements in an 'AutoRaceResults' message.
153 -- * \<RaceDate\>6/1/2014 1:00:00 PM\</RaceDate\>
155 -- * \<RaceDate\>5/24/2014 2:45:00 PM\</RaceDate\>
157 xp_datetime :: PU UTCTime
159 (to_datetime, from_datetime) `xpWrapMaybe` xpText
161 format = date_format ++ " " ++ "%-I:%M:%S %p"
163 to_datetime :: String -> Maybe UTCTime
164 to_datetime = parseTime defaultTimeLocale format
166 from_datetime :: UTCTime -> String
167 from_datetime = formatTime defaultTimeLocale format
170 -- | (Un)pickle a UTCTime from a weather forecast's gamedate. Example
173 -- \<forecast gamedate=\"Monday, December 30th\"\>
175 -- When unpickling we get rid of the suffixes \"st\", \"nd\", \"rd\", and
176 -- \"th\". During pickling, we add them back based on the last digit
179 xp_gamedate :: PU UTCTime
181 (to_gamedate, from_gamedate) `xpWrapMaybe` xpText
183 format = "%A, %B %-d"
185 to_gamedate :: String -> Maybe UTCTime
187 parseTime defaultTimeLocale format s'
189 s' = case (reverse s) of
190 (c2:c1:cs) -> let suffix = [c1,c2]
197 _ -> s -- Unknown suffix, leave it alone.
198 _ -> s -- The String is less than two characters long,
202 from_gamedate :: UTCTime -> String
203 from_gamedate d = s ++ (suffix s)
205 s = formatTime defaultTimeLocale format d
207 suffix :: String -> String
221 -- | (Un)pickle a UTCTime without the date portion.
223 xp_time :: PU UTCTime
225 (to_time, from_time) `xpWrapMaybe` xpText
227 to_time :: String -> Maybe UTCTime
228 to_time = parseTime defaultTimeLocale time_format
230 from_time :: UTCTime -> String
231 from_time = formatTime defaultTimeLocale time_format
234 -- | (Un)pickle a UTCTime without the date portion. This differs from
235 -- 'xp_time' in that it uses periods in the AM/PM part, i.e. \"A.M.\"
236 -- and \"P.M.\" It also doesn't use padding for the \"hours\" part.
240 -- * \<CurrentTimeStamp\>11:30 A.M.\</CurrentTimeStamp\>
242 xp_time_dots :: PU UTCTime
244 (to_time, from_time) `xpWrapMaybe` xpText
246 -- | The hours arent padded with zeros.
247 nopad_time_format :: String
248 nopad_time_format = "%-I:%M %p"
250 to_time :: String -> Maybe UTCTime
251 to_time = (parseTime defaultTimeLocale nopad_time_format) . (replace "." "")
253 from_time :: UTCTime -> String
255 replace "AM" "A.M." (replace "PM" "P.M." s)
257 s = formatTime defaultTimeLocale nopad_time_format t
260 -- | (Un)pickle a UTCTime without the date portion, allowing for a
261 -- value of \"TBA\" (which gets translated to 'Nothing').
265 -- A failed parse will return 'Nothing':
267 -- >>> let tn = text_node "YO"
268 -- >>> unpickleDoc xp_tba_time tn
271 -- And so will parsing a \"TBA\":
273 -- >>> let tn = text_node "TBA"
274 -- >>> unpickleDoc xp_tba_time tn
277 -- But re-pickling 'Nothing' gives only \"TBA\":
279 -- >>> pickleDoc xp_tba_time Nothing
280 -- NTree (XTag "/" []) [NTree (XText "TBA") []]
282 -- A normal time is also parsed successfully, of course:
284 -- >>> let tn = text_node "08:10 PM"
285 -- >>> unpickleDoc xp_tba_time tn
286 -- Just (Just 1970-01-01 20:10:00 UTC)
288 xp_tba_time :: PU (Maybe UTCTime)
290 (to_time, from_time) `xpWrap` xpText
292 to_time :: String -> Maybe UTCTime
294 | s == "TBA" = Nothing
295 | otherwise = parseTime defaultTimeLocale time_format s
297 from_time :: Maybe UTCTime -> String
298 from_time Nothing = "TBA"
299 from_time (Just t) = formatTime defaultTimeLocale time_format t
303 -- | (Un)pickle the \<time_stamp\> element format to/from a 'UTCTime'.
304 -- The time_stamp elements look something like,
306 -- \<time_stamp\> January 6, 2014, at 10:11 PM ET \</time_stamp\>
308 -- TSN doesn't provide a proper time zone name, so we assume that
309 -- it's always Eastern Standard Time. EST is UTC-5, so we
310 -- add/subtract 5 hours to convert to/from UTC.
314 -- >>> let tn = text_node " January 6, 2014, at 10:11 PM ET "
315 -- >>> unpickleDoc xp_time_stamp tn
316 -- Just 2014-01-07 03:11:00 UTC
318 xp_time_stamp :: PU UTCTime
320 (parse_time_stamp, from_time_stamp) `xpWrapMaybe` xpText
322 five_hours :: NominalDiffTime
323 five_hours = 5 * 60 * 60
325 subtract_five :: UTCTime -> UTCTime
326 subtract_five = addUTCTime (-1 * five_hours)
328 from_time_stamp :: UTCTime -> String
330 formatTime defaultTimeLocale time_stamp_format . subtract_five
333 -- | (Un)pickle an ambiguous 12-hour AM/PM time, which is ambiguous
334 -- because it's missing the AM/PM part.
338 -- >>> let tn = text_node "8:00"
339 -- >>> unpickleDoc xp_ambiguous_time tn
340 -- Just 1970-01-01 08:00:00 UTC
342 xp_ambiguous_time :: PU UTCTime
344 (to_time, from_time) `xpWrapMaybe` xpText
346 ambiguous_time_format :: String
347 ambiguous_time_format = "%-I:%M"
349 to_time :: String -> Maybe UTCTime
350 to_time = parseTime defaultTimeLocale ambiguous_time_format
352 from_time :: UTCTime -> String
354 formatTime defaultTimeLocale ambiguous_time_format
357 -- | Create an 'XmlTree' containing only the given text. This is
358 -- useful for testing (un)picklers, where we don't want to have to
359 -- bother to create a dummy XML document.
363 -- >>> text_node "8:00"
364 -- NTree (XText "8:00") []
366 text_node :: String -> XmlTree
367 text_node s = NTree (XText s) []
375 -- | A list of all tests for this module. This primary exists to
376 -- eliminate the unused import/export warnings for 'unpickleDoc' and
377 -- 'text_node' which are otherwise only used in the doctests.
379 pickler_tests :: TestTree
383 [ test_pickle_of_unpickle_is_identity ]
386 -- | If we unpickle something and then pickle it, we should wind up
387 -- with the same thing we started with (plus an additional root
390 test_pickle_of_unpickle_is_identity :: TestTree
391 test_pickle_of_unpickle_is_identity =
392 testCase "pickle composed with unpickle is (almost) the identity" $ do
393 let tn = text_node "8:00"
394 let (Just utctime) = unpickleDoc xp_ambiguous_time tn
395 let actual = pickleDoc xp_ambiguous_time utctime
396 let expected = NTree (XTag (mkName "/") []) [tn]