+
+
+-- | (Un)pickle an ambiguous 12-hour AM/PM time, which is ambiguous
+-- because it's missing the AM/PM part.
+--
+-- Examples:
+--
+-- >>> let tn = text_node "8:00"
+-- >>> unpickleDoc xp_ambiguous_time tn
+-- Just 1970-01-01 08:00:00 UTC
+--
+xp_ambiguous_time :: PU UTCTime
+xp_ambiguous_time =
+ (to_time, from_time) `xpWrapMaybe` xpText
+ where
+ ambiguous_time_format :: String
+ ambiguous_time_format = "%-I:%M"
+
+ to_time :: String -> Maybe UTCTime
+ to_time = parseTime defaultTimeLocale ambiguous_time_format
+
+ from_time :: UTCTime -> String
+ from_time =
+ formatTime defaultTimeLocale ambiguous_time_format
+
+
+-- | Pickle a date value from a \<date\> 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.
+--
+-- Examples:
+--
+-- >>> text_node "8:00"
+-- NTree (XText "8:00") []
+--
+text_node :: String -> XmlTree
+text_node s = NTree (XText s) []
+
+
+
+--
+-- * Tasty Tests
+--
+
+-- | A list of all tests for this module. This primary exists to
+-- eliminate the unused import/export warnings for 'unpickleDoc' and
+-- 'text_node' which are otherwise only used in the doctests.
+--
+pickler_tests :: TestTree
+pickler_tests =
+ testGroup
+ "Pickler tests"
+ [ test_pickle_of_unpickle_is_identity ]
+
+
+-- | If we unpickle something and then pickle it, we should wind up
+-- with the same thing we started with (plus an additional root
+-- element).
+--
+test_pickle_of_unpickle_is_identity :: TestTree
+test_pickle_of_unpickle_is_identity =
+ testCase "pickle composed with unpickle is (almost) the identity" $ do
+ let tn = text_node "8:00"
+ let (Just utctime) = unpickleDoc xp_ambiguous_time tn
+ let actual = pickleDoc xp_ambiguous_time utctime
+ let expected = NTree (XTag (mkName "/") []) [tn]
+ actual @?= expected