+-- | 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 ++ ")"
+
+
+