]> gitweb.michael.orlitzky.com - dead/htsn-import.git/blob - src/TSN/Picklers.hs
Update (or add) a bunch of documentation.
[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 xp_time )
8 where
9
10 -- System imports.
11 import Data.Time.Clock ( UTCTime )
12 import Data.Time.Format ( formatTime, parseTime )
13 import System.Locale ( defaultTimeLocale )
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 -- | (Un)pickle a UTCTime without the date portion.
36 --
37 xp_time :: PU UTCTime
38 xp_time =
39 (to_time, from_time) `xpWrapMaybe` xpText
40 where
41 format = "%I:%M %p"
42
43 to_time :: String -> Maybe UTCTime
44 to_time = parseTime defaultTimeLocale format
45
46 from_time :: UTCTime -> String
47 from_time = formatTime defaultTimeLocale format
48
49
50
51 -- | Parse a team_id. These are (so far!) three characters long, and
52 -- not necessarily numeric. For simplicity, we return a 'String'
53 -- rather than e.g. a @(Char, Char, Char)@. But unpickling will fail
54 -- if the team_id is longer than three characters.
55 --
56 xp_team_id :: PU String
57 xp_team_id =
58 (to_team_id, from_team_id) `xpWrapMaybe` xpText
59 where
60 to_team_id :: String -> Maybe String
61 to_team_id s
62 | length s <= 3 = Just s
63 | otherwise = Nothing
64
65 from_team_id :: String -> String
66 from_team_id = id