1 -- | (Un)picklers for data types present in The Sports Network XML
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 (
17 import Text.XML.HXT.Arrow.Pickle.Xml ( PU )
20 -- | (Un)pickle a UTCTime without the time portion.
24 (to_date, from_date) `xpWrapMaybe` xpText
28 to_date :: String -> Maybe UTCTime
29 to_date = parseTime defaultTimeLocale format
31 from_date :: UTCTime -> String
32 from_date = formatTime defaultTimeLocale format
35 -- | (Un)pickle a UTCTime from a weather forecast's gamedate. Example
38 -- \<forecast gamedate=\"Monday, December 30th\"\>
40 -- When unpickling we get rid of the suffixes \"st\", \"nd\", \"rd\", and
41 -- \"th\". During pickling, we add them back based on the last digit
44 xp_gamedate :: PU UTCTime
46 (to_gamedate, from_gamedate) `xpWrapMaybe` xpText
50 to_gamedate :: String -> Maybe UTCTime
52 parseTime defaultTimeLocale format s'
54 s' = case (reverse s) of
55 (c2:c1:cs) -> let suffix = [c1,c2]
62 _ -> s -- Unknown suffix, leave it alone.
63 _ -> s -- The String is less than two characters long,
67 from_gamedate :: UTCTime -> String
68 from_gamedate d = s ++ (suffix s)
70 s = formatTime defaultTimeLocale format d
72 suffix :: String -> String
82 -- | (Un)pickle a UTCTime without the date portion.
86 (to_time, from_time) `xpWrapMaybe` xpText
90 to_time :: String -> Maybe UTCTime
91 to_time = parseTime defaultTimeLocale format
93 from_time :: UTCTime -> String
94 from_time = formatTime defaultTimeLocale format