]> gitweb.michael.orlitzky.com - dead/htsn-import.git/blob - src/TSN/Picklers.hs
Fix pickle/unpickle of non-interger team_ids and add a test case for it.
[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 -- System imports.
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 (
16 xpText,
17 xpWrapMaybe )
18 import Text.XML.HXT.Arrow.Pickle.Xml ( PU )
19
20
21 -- | (Un)pickle a UTCTime without the time portion.
22 --
23 xp_date :: PU UTCTime
24 xp_date =
25 (to_date, from_date) `xpWrapMaybe` xpText
26 where
27 format = "%-m/%-d/%Y"
28
29 to_date :: String -> Maybe UTCTime
30 to_date = parseTime defaultTimeLocale format
31
32 from_date :: UTCTime -> String
33 from_date = formatTime defaultTimeLocale format
34
35
36 -- | Parse a team_id. These are (so far!) three characters long, and
37 -- not necessarily numeric. For simplicity, we return a 'String'
38 -- rather than e.g. a @(Char, Char, Char)@. But unpickling will fail
39 -- if the team_id is longer than three characters.
40 --
41 xp_team_id :: PU String
42 xp_team_id =
43 (to_team_id, from_team_id) `xpWrapMaybe` xpText
44 where
45 to_team_id :: String -> Maybe String
46 to_team_id s
47 | length s <= 3 = Just s
48 | otherwise = Nothing
49
50 from_team_id :: String -> String
51 from_team_id = id