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 )
29 -- | (Un)pickle a UTCTime without the time portion.
33 (to_date, from_date) `xpWrapMaybe` xpText
37 to_date :: String -> Maybe UTCTime
38 to_date = parseTime defaultTimeLocale format
40 from_date :: UTCTime -> String
41 from_date = formatTime defaultTimeLocale format
44 -- | (Un)pickle a UTCTime from a weather forecast's gamedate. Example
47 -- \<forecast gamedate=\"Monday, December 30th\"\>
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
53 xp_gamedate :: PU UTCTime
55 (to_gamedate, from_gamedate) `xpWrapMaybe` xpText
59 to_gamedate :: String -> Maybe UTCTime
61 parseTime defaultTimeLocale format s'
63 s' = case (reverse s) of
64 (c2:c1:cs) -> let suffix = [c1,c2]
71 _ -> s -- Unknown suffix, leave it alone.
72 _ -> s -- The String is less than two characters long,
76 from_gamedate :: UTCTime -> String
77 from_gamedate d = s ++ (suffix s)
79 s = formatTime defaultTimeLocale format d
81 suffix :: String -> String
95 -- | (Un)pickle a UTCTime without the date portion.
99 (to_time, from_time) `xpWrapMaybe` xpText
101 to_time :: String -> Maybe UTCTime
102 to_time = parseTime defaultTimeLocale time_format
104 from_time :: UTCTime -> String
105 from_time = formatTime defaultTimeLocale time_format
108 -- | (Un)pickle a UTCTime without the date portion, allowing for a
109 -- value of \"TBA\" (which gets translated to 'Nothing').
111 xp_tba_time :: PU (Maybe UTCTime)
113 (to_time, from_time) `xpWrap` xpText
115 to_time :: String -> Maybe UTCTime
117 | s == "TBA" = Nothing
118 | otherwise = parseTime defaultTimeLocale time_format s
120 from_time :: Maybe UTCTime -> String
121 from_time Nothing = ""
122 from_time (Just t) = formatTime defaultTimeLocale time_format t
126 -- | (Un)pickle the \<time_stamp\> element format to/from a 'UTCTime'.
128 -- Example: \<time_stamp\> January 6, 2014, at 10:11 PM ET \</time_stamp\>
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.
134 xp_time_stamp :: PU UTCTime
136 (parse_time_stamp, from_time_stamp) `xpWrapMaybe` xpText
138 five_hours :: NominalDiffTime
139 five_hours = 5 * 60 * 60
141 subtract_five :: UTCTime -> UTCTime
142 subtract_five = addUTCTime (-1 * five_hours)
144 from_time_stamp :: UTCTime -> String
146 formatTime defaultTimeLocale time_stamp_format . subtract_five