1 -- | (Un)picklers for data types present in The Sports Network XML
12 import Data.Time.Clock ( NominalDiffTime, UTCTime, addUTCTime )
13 import Data.Time.Format ( formatTime, parseTime )
14 import System.Locale ( defaultTimeLocale )
15 import Text.XML.HXT.Arrow.Pickle (
18 import Text.XML.HXT.Arrow.Pickle.Xml ( PU )
21 -- | (Un)pickle a UTCTime without the time portion.
25 (to_date, from_date) `xpWrapMaybe` xpText
29 to_date :: String -> Maybe UTCTime
30 to_date = parseTime defaultTimeLocale format
32 from_date :: UTCTime -> String
33 from_date = formatTime defaultTimeLocale format
36 -- | (Un)pickle a UTCTime from a weather forecast's gamedate. Example
39 -- \<forecast gamedate=\"Monday, December 30th\"\>
41 -- When unpickling we get rid of the suffixes \"st\", \"nd\", \"rd\", and
42 -- \"th\". During pickling, we add them back based on the last digit
45 xp_gamedate :: PU UTCTime
47 (to_gamedate, from_gamedate) `xpWrapMaybe` xpText
51 to_gamedate :: String -> Maybe UTCTime
53 parseTime defaultTimeLocale format s'
55 s' = case (reverse s) of
56 (c2:c1:cs) -> let suffix = [c1,c2]
63 _ -> s -- Unknown suffix, leave it alone.
64 _ -> s -- The String is less than two characters long,
68 from_gamedate :: UTCTime -> String
69 from_gamedate d = s ++ (suffix s)
71 s = formatTime defaultTimeLocale format d
73 suffix :: String -> String
84 -- | The time format string used in 'xp_time' and 'xp_time_stamp'.
86 xp_time_format :: String
87 xp_time_format = "%I:%M %p"
90 -- | (Un)pickle a UTCTime without the date portion.
94 (to_time, from_time) `xpWrapMaybe` xpText
96 to_time :: String -> Maybe UTCTime
97 to_time = parseTime defaultTimeLocale xp_time_format
99 from_time :: UTCTime -> String
100 from_time = formatTime defaultTimeLocale xp_time_format
104 -- | (Un)pickle the \<time_stamp\> element format to/from a 'UTCTime'.
106 -- Example: \<time_stamp\> January 6, 2014, at 10:11 PM ET \</time_stamp\>
108 -- TSN doesn't provide a proper time zone name, so we assume that
109 -- it's always Eastern Standard Time. EST is UTC-5, so we
110 -- add/subtract 5 hours to convert to/from UTC.
112 xp_time_stamp :: PU UTCTime
114 (to_time_stamp, from_time_stamp) `xpWrapMaybe` xpText
116 -- This omits the timezone and trailing space.
117 format = " %B %-d, %Y, at " ++ xp_time_format ++ " ET "
119 five_hours :: NominalDiffTime
120 five_hours = 5 * 60 * 60
122 add_five :: UTCTime -> UTCTime
123 add_five = addUTCTime five_hours
125 subtract_five :: UTCTime -> UTCTime
126 subtract_five = addUTCTime (-1 * five_hours)
128 to_time_stamp :: String -> Maybe UTCTime
129 to_time_stamp = fmap add_five . parseTime defaultTimeLocale format
131 from_time_stamp :: UTCTime -> String
132 from_time_stamp = formatTime defaultTimeLocale format . subtract_five