X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=src%2FTSN%2FPicklers.hs;h=3d7215a07744436055a7eff73a8169201d394301;hb=c024d97dd3686c140de40a039b1298d2a7882506;hp=a64db8a2023760375ac467d888b45f96294d7322;hpb=acd67d75aab3f2350b488fe6402bc0a20e476a18;p=dead%2Fhtsn-import.git diff --git a/src/TSN/Picklers.hs b/src/TSN/Picklers.hs index a64db8a..3d7215a 100644 --- a/src/TSN/Picklers.hs +++ b/src/TSN/Picklers.hs @@ -7,6 +7,7 @@ module TSN.Picklers ( xp_date, xp_date_padded, xp_datetime, + xp_early_line_date, xp_earnings, xp_gamedate, xp_tba_time, @@ -16,13 +17,15 @@ module TSN.Picklers ( where -- System imports. +import Data.Char ( toUpper ) 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.Format ( formatTime, parseTime ) import Data.Tree.NTree.TypeDefs ( NTree(..) ) -import System.Locale ( defaultTimeLocale ) +import System.Locale ( TimeLocale( wDays, months ), defaultTimeLocale ) import Test.Tasty ( TestTree, testGroup ) import Test.Tasty.HUnit ( (@?=), testCase ) import Text.XML.HXT.Arrow.Pickle ( @@ -104,6 +107,10 @@ xp_date = -- >>> unpickleDoc xp_date_padded tn -- Just 1983-02-15 00:00:00 UTC -- +-- >>> let tn = text_node "06/07/2014" +-- >>> unpickleDoc xp_date_padded tn +-- Just 2014-06-07 00:00:00 UTC +-- xp_date_padded :: PU UTCTime xp_date_padded = (to_date, from_date) `xpWrapMaybe` xpText @@ -211,6 +218,37 @@ xp_datetime = from_datetime = formatTime defaultTimeLocale format + +-- | Takes a 'UTCTime', and returns the English suffix that would be +-- appropriate after the day of the month. For example, if we have a +-- UTCTime representing Christmas, this would return \"th\" because +-- \"th\" is the right suffix of \"December 25th\". +-- +-- Examples: +-- +-- >>> import Data.Maybe ( fromJust ) +-- >>> :{ +-- let parse_date :: String -> Maybe UTCTime; +-- parse_date = parseTime defaultTimeLocale date_format; +-- :} +-- +-- >>> let dates = [ "1/" ++ (d : "/1970") | d <- ['1'..'9'] ] +-- >>> let suffixes = map (date_suffix . fromJust . parse_date) dates +-- >>> suffixes +-- ["st","nd","rd","th","th","th","th","th","th"] +-- +date_suffix :: UTCTime -> String +date_suffix t = + case (reverse daystr) of + [] -> [] + ('1':_) -> "st" + ('2':_) -> "nd" + ('3':_) -> "rd" + _ -> "th" + where + daystr = formatTime defaultTimeLocale "%d" t + + -- | (Un)pickle a UTCTime from a weather forecast's gamedate. Example -- input looks like, -- @@ -240,29 +278,19 @@ xp_gamedate = s' = case (reverse s) of (c2:c1:cs) -> let suffix = [c1,c2] in - case suffix of - "st" -> reverse cs - "nd" -> reverse cs - "rd" -> reverse cs - "th" -> reverse cs - _ -> s -- Unknown suffix, leave it alone. + if suffix `elem` ["st","nd","rd","th"] + then reverse cs + else s -- Unknown suffix, leave it alone. + _ -> s -- The String is less than two characters long, -- leave it alone. from_gamedate :: UTCTime -> String - from_gamedate d = s ++ (suffix s) + from_gamedate d = s ++ (date_suffix d) where s = formatTime defaultTimeLocale format d - suffix :: String -> String - suffix cs = - case (reverse cs) of - [] -> [] - ('1':_) -> "st" - ('2':_) -> "nd" - ('3':_) -> "rd" - _ -> "th" @@ -433,6 +461,69 @@ xp_ambiguous_time = formatTime defaultTimeLocale ambiguous_time_format +-- | Pickle a date value from a \ element as they appear in the +-- early lines. This is a particularly wacky format, but then so is +-- the associated time (see 'xp_ambiguous_time'). +-- +-- Examples: +-- +-- >>> let tn = text_node "SUNDAY, MAY 25TH (05/25/2014)" +-- >>> let (Just result) = unpickleDoc xp_early_line_date tn +-- >>> result +-- 2014-05-25 00:00:00 UTC +-- >>> pickleDoc xp_early_line_date result +-- NTree (XTag "/" []) [NTree (XText "SUNDAY, MAY 25TH (05/25/2014)") []] +-- +-- >>> let tn = text_node "SATURDAY, JUNE 7TH (06/07/2014)" +-- >>> let (Just result) = unpickleDoc xp_early_line_date tn +-- >>> result +-- 2014-06-07 00:00:00 UTC +-- >>> pickleDoc xp_early_line_date result +-- NTree (XTag "/" []) [NTree (XText "SATURDAY, JUNE 7TH (06/07/2014)") []] +-- +xp_early_line_date :: PU UTCTime +xp_early_line_date = + (to_time, from_time) `xpWrapMaybe` xpText + where + -- | We need to create our own time locale that talks IN ALL CAPS. + -- Actually, 'parseTime' doesn't seem to care about the + -- case. But when we spit it back out again ('formatTime'), + -- we'll want it to be in all caps. + -- + caps_time_locale :: TimeLocale + caps_time_locale = + defaultTimeLocale { wDays = caps_days, months = caps_months } + + caps_days :: [(String,String)] + caps_days = map both_to_upper (wDays defaultTimeLocale) + + caps_months :: [(String,String)] + caps_months = map both_to_upper (months defaultTimeLocale) + + both_to_upper :: (String,String) -> (String,String) + both_to_upper (s1,s2) = (map toUpper s1, map toUpper s2) + + wacko_date_formats :: [String] + wacko_date_formats = + ["%A, %B %-d" ++ suffix ++ " (" ++ date_format_padded ++ ")" | + suffix <- ["ST", "ND", "RD","TH"] ] + + to_time :: String -> Maybe UTCTime + to_time s = + listToMaybe $ catMaybes possible_parses + where + possible_parses = [ parseTime caps_time_locale fmt s | + fmt <- wacko_date_formats ] + + from_time :: UTCTime -> String + from_time t = + formatTime caps_time_locale fmt t + where + upper_suffix = map toUpper (date_suffix t) + fmt = "%A, %B %-d" ++ upper_suffix ++ " (" ++ date_format_padded ++ ")" + + + -- | Create an 'XmlTree' containing only the given text. This is -- useful for testing (un)picklers, where we don't want to have to -- bother to create a dummy XML document.