module TSN.Picklers (
xp_date,
xp_gamedate,
- xp_time )
+ xp_time,
+ xp_time_stamp )
where
-- System imports.
-import Data.Time.Clock ( UTCTime )
+import Data.Time.Clock ( NominalDiffTime, UTCTime, addUTCTime )
import Data.Time.Format ( formatTime, parseTime )
import System.Locale ( defaultTimeLocale )
import Text.XML.HXT.Arrow.Pickle (
_ -> "th"
+
+-- | The time format string used in 'xp_time' and 'xp_time_stamp'.
+--
+xp_time_format :: String
+xp_time_format = "%I:%M %p"
+
+
-- | (Un)pickle a UTCTime without the date portion.
--
xp_time :: PU UTCTime
xp_time =
(to_time, from_time) `xpWrapMaybe` xpText
where
- format = "%I:%M %p"
-
to_time :: String -> Maybe UTCTime
- to_time = parseTime defaultTimeLocale format
+ to_time = parseTime defaultTimeLocale xp_time_format
from_time :: UTCTime -> String
- from_time = formatTime defaultTimeLocale format
+ from_time = formatTime defaultTimeLocale xp_time_format
+
+
+
+-- | (Un)pickle the \<time_stamp\> element format to/from a 'UTCTime'.
+--
+-- Example: \<time_stamp\> January 6, 2014, at 10:11 PM ET \</time_stamp\>
+--
+-- TSN doesn't provide a proper time zone name, so we assume that
+-- it's always Eastern Standard Time. EST is UTC-5, so we
+-- add/subtract 5 hours to convert to/from UTC.
+--
+xp_time_stamp :: PU UTCTime
+xp_time_stamp =
+ (to_time_stamp, from_time_stamp) `xpWrapMaybe` xpText
+ where
+ -- This omits the timezone and trailing space.
+ format = " %B %-d, %Y, at " ++ xp_time_format ++ " ET "
+
+ five_hours :: NominalDiffTime
+ five_hours = 5 * 60 * 60
+
+ add_five :: UTCTime -> UTCTime
+ add_five = addUTCTime five_hours
+
+ subtract_five :: UTCTime -> UTCTime
+ subtract_five = addUTCTime (-1 * five_hours)
+
+ to_time_stamp :: String -> Maybe UTCTime
+ to_time_stamp = fmap add_five . parseTime defaultTimeLocale format
+ from_time_stamp :: UTCTime -> String
+ from_time_stamp = formatTime defaultTimeLocale format . subtract_five