From 6eb1c7477c2d4d3cace6d1b865a5efbec21300a7 Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Fri, 25 Jul 2014 16:59:59 -0400 Subject: [PATCH] Use UTC instead of EST to store the timestamps; they're both wrong anyway. --- doc/man1/htsn-import.1 | 8 +++++--- src/TSN/Parse.hs | 30 +++++++++++++++--------------- src/TSN/Picklers.hs | 27 ++++++++++++++------------- src/TSN/XML/GameInfo.hs | 2 +- src/TSN/XML/SportInfo.hs | 2 +- 5 files changed, 36 insertions(+), 33 deletions(-) diff --git a/doc/man1/htsn-import.1 b/doc/man1/htsn-import.1 index 34a8bbe..f07d0f1 100644 --- a/doc/man1/htsn-import.1 +++ b/doc/man1/htsn-import.1 @@ -381,9 +381,11 @@ The \(dqtime zone\(dq is given as \(dqET\(dq, but unfortunately \(dqET\(dq is not a valid time zone. It stands for \(dqEastern Time\(dq, which can belong to either of two time zones, EST or EDT, based on the time of the year (that is, whether or not daylight -savings time is in effect). Since we can't tell from the timestamp, we -always parse these as EST which is UTC-5. When daylight savings is in -effect, they will be off by an hour. +savings time is in effect) and one's location (for example, Arizona +doesn't observe daylight savings time). It's not much more useful to +be off by one hour than it is to be off by five hours, and since we +can't determine the true offset from the timestamp, we always parse +and store these as UTC. Here's a list of the ones that may cause surprises: diff --git a/src/TSN/Parse.hs b/src/TSN/Parse.hs index 0d497b4..54175d7 100644 --- a/src/TSN/Parse.hs +++ b/src/TSN/Parse.hs @@ -8,7 +8,7 @@ module TSN.Parse ( where import Data.Either.Utils ( maybeToEither ) -import Data.Time.Clock ( NominalDiffTime, UTCTime, addUTCTime ) +import Data.Time.Clock ( UTCTime ) import Data.Time.Format ( parseTime ) import System.Locale ( defaultTimeLocale ) import Text.Read ( readMaybe ) @@ -89,27 +89,27 @@ parse_xmlfid xmltree = time_format :: String time_format = "%I:%M %p" --- | The format string for a time_stamp. This omits the leading and --- trailing space. + +-- | The format string for a time_stamp. We keep the leading/trailing +-- space so that parseTime and formatTime are inverses are one +-- another, even though there is some confusion as to how these two +-- functions should behave: +-- +-- +-- time_stamp_format :: String -time_stamp_format = "%B %-d, %Y, at " ++ time_format ++ " ET" +time_stamp_format = " %B %-d, %Y, at " ++ time_format ++ " ET " --- | Parse a time stamp from a 'String' (maybe). --- --- 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 five hours to convert to UTC. + +-- | Parse a time stamp from a 'String' (maybe). TSN doesn't provide a +-- proper time zone name, so we parse it as UTC, and maybe our +-- eventual consumer can figure out a way to deduce the time zone. -- parse_time_stamp :: String -> Maybe UTCTime parse_time_stamp = - fmap add_five . parseTime defaultTimeLocale time_stamp_format - where - five_hours :: NominalDiffTime - five_hours = 5 * 60 * 60 + parseTime defaultTimeLocale time_stamp_format - add_five :: UTCTime -> UTCTime - add_five = addUTCTime five_hours -- | Extract the \"time_stamp\" element from a document. If we fail diff --git a/src/TSN/Picklers.hs b/src/TSN/Picklers.hs index 6f131a7..09c3a3b 100644 --- a/src/TSN/Picklers.hs +++ b/src/TSN/Picklers.hs @@ -23,7 +23,7 @@ import Data.List ( intercalate ) import Data.List.Split ( chunksOf ) import Data.Maybe ( catMaybes, listToMaybe ) import Data.String.Utils ( replace ) -import Data.Time.Clock ( NominalDiffTime, UTCTime, addUTCTime ) +import Data.Time.Clock ( UTCTime ) import Data.Time.Format ( formatTime, parseTime ) import Data.Tree.NTree.TypeDefs ( NTree(..) ) import System.Locale ( TimeLocale( wDays, months ), defaultTimeLocale ) @@ -454,29 +454,30 @@ xp_tba_time = -- -- \ 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. +-- TSN doesn't provide a proper time zone name, only \"ET\" for +-- \"Eastern Time\". But \"Eastern Time\" changes throughout the +-- year, depending on one's location, for daylight-savings +-- time. It's really not any more useful to be off by one hour than +-- it is to be off by 5 hours, so rather than guess at EDT/EST, we +-- just store the timestamp as UTC. -- -- Examples: -- -- >>> let tn = text_node " January 6, 2014, at 10:11 PM ET " --- >>> unpickleDoc xp_time_stamp tn --- Just 2014-01-07 03:11:00 UTC +-- >>> let (Just tstamp) = unpickleDoc xp_time_stamp tn +-- >>> tstamp +-- 2014-01-06 22:11:00 UTC +-- >>> pickleDoc xp_time_stamp tstamp +-- NTree (XTag "/" []) [NTree (XText " January 6, 2014, at 10:11 PM ET ") []] -- xp_time_stamp :: PU UTCTime xp_time_stamp = (parse_time_stamp, from_time_stamp) `xpWrapMaybe` xpText where - five_hours :: NominalDiffTime - five_hours = 5 * 60 * 60 - - subtract_five :: UTCTime -> UTCTime - subtract_five = addUTCTime (-1 * five_hours) - from_time_stamp :: UTCTime -> String from_time_stamp = - formatTime defaultTimeLocale time_stamp_format . subtract_five + formatTime defaultTimeLocale time_stamp_format + -- | (Un)pickle an ambiguous 12-hour AM/PM time, which is ambiguous diff --git a/src/TSN/XML/GameInfo.hs b/src/TSN/XML/GameInfo.hs index b7c4c5b..2b5e1ad 100644 --- a/src/TSN/XML/GameInfo.hs +++ b/src/TSN/XML/GameInfo.hs @@ -162,7 +162,7 @@ test_accessors = testCase "we can access a parsed game_info" $ do let a2 = xml_file_id t let ex2 = 21201550 let a3 = show $ time_stamp t - let ex3 = "2014-05-31 20:13:00 UTC" + let ex3 = "2014-05-31 15:13:00 UTC" let a4 = take 9 (xml t) let ex4 = "" let actual = (a1,a2,a3,a4) diff --git a/src/TSN/XML/SportInfo.hs b/src/TSN/XML/SportInfo.hs index 187796c..6bb99a2 100644 --- a/src/TSN/XML/SportInfo.hs +++ b/src/TSN/XML/SportInfo.hs @@ -275,7 +275,7 @@ test_accessors = testCase "we can access a parsed sport_info" $ do let a2 = xml_file_id t let ex2 = 2011 let a3 = show $ time_stamp t - let ex3 = "2009-09-28 00:50:00 UTC" + let ex3 = "2009-09-27 19:50:00 UTC" let a4 = take 9 (xml t) let ex4 = "" let actual = (a1,a2,a3,a4) -- 2.43.2