]> gitweb.michael.orlitzky.com - dead/htsn-import.git/blob - src/TSN/Picklers.hs
Add a minor TODO note.
[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_gamedate,
7 xp_tba_time,
8 xp_time,
9 xp_time_stamp )
10 where
11
12 -- System imports.
13 import Data.Time.Clock ( NominalDiffTime, UTCTime, addUTCTime )
14 import Data.Time.Format ( formatTime, parseTime )
15 import System.Locale ( defaultTimeLocale )
16 import Text.XML.HXT.Arrow.Pickle (
17 xpText,
18 xpWrap,
19 xpWrapMaybe )
20 import Text.XML.HXT.Arrow.Pickle.Xml ( PU )
21
22 -- Local imports.
23 import TSN.Parse (
24 parse_time_stamp,
25 time_format,
26 time_stamp_format )
27
28
29 -- | (Un)pickle a UTCTime without the time portion.
30 --
31 xp_date :: PU UTCTime
32 xp_date =
33 (to_date, from_date) `xpWrapMaybe` xpText
34 where
35 format = "%-m/%-d/%Y"
36
37 to_date :: String -> Maybe UTCTime
38 to_date = parseTime defaultTimeLocale format
39
40 from_date :: UTCTime -> String
41 from_date = formatTime defaultTimeLocale format
42
43
44 -- | (Un)pickle a UTCTime from a weather forecast's gamedate. Example
45 -- input looks like,
46 --
47 -- \<forecast gamedate=\"Monday, December 30th\"\>
48 --
49 -- When unpickling we get rid of the suffixes \"st\", \"nd\", \"rd\", and
50 -- \"th\". During pickling, we add them back based on the last digit
51 -- of the date.
52 --
53 xp_gamedate :: PU UTCTime
54 xp_gamedate =
55 (to_gamedate, from_gamedate) `xpWrapMaybe` xpText
56 where
57 format = "%A, %B %-d"
58
59 to_gamedate :: String -> Maybe UTCTime
60 to_gamedate s =
61 parseTime defaultTimeLocale format s'
62 where
63 s' = case (reverse s) of
64 (c2:c1:cs) -> let suffix = [c1,c2]
65 in
66 case suffix of
67 "st" -> reverse cs
68 "nd" -> reverse cs
69 "rd" -> reverse cs
70 "th" -> reverse cs
71 _ -> s -- Unknown suffix, leave it alone.
72 _ -> s -- The String is less than two characters long,
73 -- leave it alone.
74
75
76 from_gamedate :: UTCTime -> String
77 from_gamedate d = s ++ (suffix s)
78 where
79 s = formatTime defaultTimeLocale format d
80
81 suffix :: String -> String
82 suffix cs =
83 case (reverse cs) of
84 [] -> []
85 ('1':_) -> "st"
86 ('2':_) -> "nd"
87 ('3':_) -> "rd"
88 _ -> "th"
89
90
91
92
93
94
95 -- | (Un)pickle a UTCTime without the date portion.
96 --
97 xp_time :: PU UTCTime
98 xp_time =
99 (to_time, from_time) `xpWrapMaybe` xpText
100 where
101 to_time :: String -> Maybe UTCTime
102 to_time = parseTime defaultTimeLocale time_format
103
104 from_time :: UTCTime -> String
105 from_time = formatTime defaultTimeLocale time_format
106
107
108 -- | (Un)pickle a UTCTime without the date portion, allowing for a
109 -- value of \"TBA\" (which gets translated to 'Nothing').
110 --
111 xp_tba_time :: PU (Maybe UTCTime)
112 xp_tba_time =
113 (to_time, from_time) `xpWrap` xpText
114 where
115 to_time :: String -> Maybe UTCTime
116 to_time s
117 | s == "TBA" = Nothing
118 | otherwise = parseTime defaultTimeLocale time_format s
119
120 from_time :: Maybe UTCTime -> String
121 from_time Nothing = ""
122 from_time (Just t) = formatTime defaultTimeLocale time_format t
123
124
125
126 -- | (Un)pickle the \<time_stamp\> element format to/from a 'UTCTime'.
127 --
128 -- Example: \<time_stamp\> January 6, 2014, at 10:11 PM ET \</time_stamp\>
129 --
130 -- TSN doesn't provide a proper time zone name, so we assume that
131 -- it's always Eastern Standard Time. EST is UTC-5, so we
132 -- add/subtract 5 hours to convert to/from UTC.
133 --
134 xp_time_stamp :: PU UTCTime
135 xp_time_stamp =
136 (parse_time_stamp, from_time_stamp) `xpWrapMaybe` xpText
137 where
138 five_hours :: NominalDiffTime
139 five_hours = 5 * 60 * 60
140
141 subtract_five :: UTCTime -> UTCTime
142 subtract_five = addUTCTime (-1 * five_hours)
143
144 from_time_stamp :: UTCTime -> String
145 from_time_stamp =
146 formatTime defaultTimeLocale time_stamp_format . subtract_five