]> gitweb.michael.orlitzky.com - dead/htsn-import.git/blob - src/TSN/Picklers.hs
Remove the XmlPickler instance for Bool.
[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 xp_date,
6 xp_team_id )
7 where
8
9 import Data.Time.Clock ( UTCTime )
10 import Data.Time.Format ( formatTime, parseTime )
11 import System.Locale ( defaultTimeLocale )
12 import Text.Printf ( printf )
13 import Text.Read ( readMaybe )
14 import Text.XML.HXT.Arrow.Pickle (
15 xpText,
16 xpWrapMaybe )
17 import Text.XML.HXT.Arrow.Pickle.Xml ( PU )
18
19
20 -- | (Un)pickle a UTCTime without the time portion.
21 --
22 xp_date :: PU UTCTime
23 xp_date =
24 (to_date, from_date) `xpWrapMaybe` xpText
25 where
26 format = "%-m/%-d/%Y"
27
28 to_date :: String -> Maybe UTCTime
29 to_date = parseTime defaultTimeLocale format
30
31 from_date :: UTCTime -> String
32 from_date = formatTime defaultTimeLocale format
33
34
35 -- | Parse a team_id. This *should* just be an 'Int', but TSN is doing
36 -- something weird. First of all, player IDs do look like normal
37 -- 'Int's. But the team IDs are all stuck in the triple digits, and
38 -- double-digit team IDs appear to be padded to three characters
39 -- with a leading '0'. So maybe they're treating these as text?
40 --
41 -- In any case, we do the simplest thing that is correct for all the
42 -- XML we've got: pad it to (only) three digits on pickling.
43 --
44 xp_team_id :: PU Int
45 xp_team_id =
46 (to_team_id, from_team_id) `xpWrapMaybe` xpText
47 where
48 to_team_id :: String -> Maybe Int
49 to_team_id = readMaybe
50
51 from_team_id :: Int -> String
52 from_team_id = printf "%03d"