From 5473881a0fc914b8d9df1cddce2d4237c92b4bcf Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Mon, 20 Jan 2014 02:07:20 -0500 Subject: [PATCH] Add a pickler for the elements. --- src/TSN/Picklers.hs | 49 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 43 insertions(+), 6 deletions(-) diff --git a/src/TSN/Picklers.hs b/src/TSN/Picklers.hs index 72ab455..f80b085 100644 --- a/src/TSN/Picklers.hs +++ b/src/TSN/Picklers.hs @@ -4,11 +4,12 @@ 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 ( @@ -79,17 +80,53 @@ xp_gamedate = _ -> "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 \ element format to/from a 'UTCTime'. +-- +-- Example: \ January 6, 2014, at 10:11 PM ET \ +-- +-- 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 -- 2.44.2