1 -- | (Un)picklers for data types present in The Sports Network XML
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 (
20 import Text.XML.HXT.Arrow.Pickle.Xml ( PU )
23 -- | (Un)pickle a UTCTime without the time portion.
27 (to_date, from_date) `xpWrapMaybe` xpText
31 to_date :: String -> Maybe UTCTime
32 to_date = parseTime defaultTimeLocale format
34 from_date :: UTCTime -> String
35 from_date = formatTime defaultTimeLocale format
38 -- | (Un)pickle a UTCTime from a weather forecast's gamedate. Example
41 -- \<forecast gamedate=\"Monday, December 30th\"\>
43 -- When unpickling we get rid of the suffixes \"st\", \"nd\", \"rd\", and
44 -- \"th\". During pickling, we add them back based on the last digit
47 xp_gamedate :: PU UTCTime
49 (to_gamedate, from_gamedate) `xpWrapMaybe` xpText
53 to_gamedate :: String -> Maybe UTCTime
55 parseTime defaultTimeLocale format s'
57 s' = case (reverse s) of
58 (c2:c1:cs) -> let suffix = [c1,c2]
65 _ -> s -- Unknown suffix, leave it alone.
66 _ -> s -- The String is less than two characters long,
70 from_gamedate :: UTCTime -> String
71 from_gamedate d = s ++ (suffix s)
73 s = formatTime defaultTimeLocale format d
75 suffix :: String -> String
86 -- | The time format string used in 'xp_time' and 'xp_time_stamp'.
88 xp_time_format :: String
89 xp_time_format = "%I:%M %p"
92 -- | (Un)pickle a UTCTime without the date portion.
96 (to_time, from_time) `xpWrapMaybe` xpText
98 to_time :: String -> Maybe UTCTime
99 to_time = parseTime defaultTimeLocale xp_time_format
101 from_time :: UTCTime -> String
102 from_time = formatTime defaultTimeLocale xp_time_format
105 -- | (Un)pickle a UTCTime without the date portion, allowing for a
106 -- value of \"TBA\" (which gets translated to 'Nothing').
108 xp_tba_time :: PU (Maybe UTCTime)
110 (to_time, from_time) `xpWrap` xpText
112 to_time :: String -> Maybe UTCTime
114 | s == "TBA" = Nothing
115 | otherwise = parseTime defaultTimeLocale xp_time_format s
117 from_time :: Maybe UTCTime -> String
118 from_time Nothing = ""
119 from_time (Just t) = formatTime defaultTimeLocale xp_time_format t
122 -- | (Un)pickle the \<time_stamp\> element format to/from a 'UTCTime'.
124 -- Example: \<time_stamp\> January 6, 2014, at 10:11 PM ET \</time_stamp\>
126 -- TSN doesn't provide a proper time zone name, so we assume that
127 -- it's always Eastern Standard Time. EST is UTC-5, so we
128 -- add/subtract 5 hours to convert to/from UTC.
130 xp_time_stamp :: PU UTCTime
132 (to_time_stamp, from_time_stamp) `xpWrapMaybe` xpText
134 -- This omits the timezone and trailing space.
135 format = "%B %-d, %Y, at " ++ xp_time_format ++ " ET"
137 five_hours :: NominalDiffTime
138 five_hours = 5 * 60 * 60
140 add_five :: UTCTime -> UTCTime
141 add_five = addUTCTime five_hours
143 subtract_five :: UTCTime -> UTCTime
144 subtract_five = addUTCTime (-1 * five_hours)
146 to_time_stamp :: String -> Maybe UTCTime
147 to_time_stamp = fmap add_five . parseTime defaultTimeLocale format
149 from_time_stamp :: UTCTime -> String
150 from_time_stamp = formatTime defaultTimeLocale format . subtract_five