1 -- | (Un)picklers for data types present in The Sports Network XML
10 import Data.Time.Clock ( UTCTime )
11 import Data.Time.Format ( formatTime, parseTime )
12 import System.Locale ( defaultTimeLocale )
13 import Text.Printf ( printf )
14 import Text.Read ( readMaybe )
15 import Text.XML.HXT.Arrow.Pickle (
18 import Text.XML.HXT.Arrow.Pickle.Xml ( PU )
21 -- | (Un)pickle a UTCTime without the time portion.
25 (to_date, from_date) `xpWrapMaybe` xpText
29 to_date :: String -> Maybe UTCTime
30 to_date = parseTime defaultTimeLocale format
32 from_date :: UTCTime -> String
33 from_date = formatTime defaultTimeLocale format
36 -- | Parse a team_id. This /should/ just be an 'Int', but TSN is doing
37 -- something weird. First of all, player IDs do look like normal
38 -- 'Int's. But the team IDs are all stuck in the triple digits, and
39 -- double-digit team IDs appear to be padded to three characters
40 -- with a leading '0'. So maybe they're treating these as text?
42 -- In any case, we do the simplest thing that is correct for all the
43 -- XML we've got: pad it to (only) three digits on pickling.
47 (to_team_id, from_team_id) `xpWrapMaybe` xpText
49 to_team_id :: String -> Maybe Int
50 to_team_id = readMaybe
52 from_team_id :: Int -> String
53 from_team_id = printf "%03d"