xp_datetime,
xp_early_line_date,
xp_earnings,
+ xp_fracpart_only_double,
xp_gamedate,
xp_tba_time,
xp_time,
import System.Locale ( TimeLocale( wDays, months ), defaultTimeLocale )
import Test.Tasty ( TestTree, testGroup )
import Test.Tasty.HUnit ( (@?=), testCase )
+import Text.Read ( readMaybe )
import Text.XML.HXT.Arrow.Pickle (
xpText,
xpWrap,
-- >>> 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
reverse (intercalate "," $ chunksOf 3 $ reverse $ show x)
+
-- | Parse \<Earnings\> from an 'AutoRaceResultsListing'. These are
-- essentially 'Int's, but they look like,
--
+-- | Pickle a 'Double' that can be missing its leading zero (for
+-- values less than one). For example, we've seen,
+--
+-- <TrackLength KPH=".805">0.5</TrackLength>
+--
+-- Which 'xpPrim' can't handle without the leading
+-- zero. Unfortunately there's no way pickle/unpickle can be
+-- inverses of each other here, since \"0.5\" and \".5\" should
+-- unpickle to the same 'Double'.
+--
+-- Examples:
+--
+-- >>> let tn = text_node "0.5"
+-- >>> unpickleDoc xp_fracpart_only_double tn
+-- Just 0.5
+--
+-- >>> let tn = text_node ".5"
+-- >>> unpickleDoc xp_fracpart_only_double tn
+-- Just 0.5
+--
+-- >>> let tn = text_node "foo"
+-- >>> unpickleDoc xp_fracpart_only_double tn
+-- Nothing
+--
+xp_fracpart_only_double :: PU Double
+xp_fracpart_only_double =
+ (to_double, from_double) `xpWrapMaybe` xpText
+ where
+ -- | Convert a 'String' to a 'Double', maybe. We always prepend a
+ -- zero, since it will fix the fraction-only values, and not hurt
+ -- the ones that already have a leading integer.
+ to_double :: String -> Maybe Double
+ to_double s = readMaybe ("0" ++ s)
+
+ from_double :: Double -> String
+ from_double = show
+
+
+
-- | (Un)pickle an unpadded 'UTCTime'. Used for example on the
-- \<RaceDate\> elements in an 'AutoRaceResults' message.
--
--
-- Examples:
--
+-- >>> import Data.Maybe ( fromJust )
-- >>> :{
-- let parse_date :: String -> Maybe UTCTime;
-- parse_date = parseTime defaultTimeLocale date_format;
-- :}
--
--- >>> let (Just d1) = parse_date "1/1/1970"
--- >>> date_suffix d1
--- "st"
---
--- >>> let (Just d2) = parse_date "1/2/1970"
--- >>> date_suffix d2
--- "nd"
---
--- >>> let (Just d3) = parse_date "1/3/1970"
--- >>> date_suffix d3
--- "rd"
---
--- >>> let (Just d4) = parse_date "1/4/1970"
--- >>> date_suffix d4
--- "th"
+-- >>> 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 =
-- >>> 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
wacko_date_formats :: [String]
wacko_date_formats =
- ["%A, %B %d" ++ suffix ++ " (" ++ date_format_padded ++ ")" |
+ ["%A, %B %-d" ++ suffix ++ " (" ++ date_format_padded ++ ")" |
suffix <- ["ST", "ND", "RD","TH"] ]
to_time :: String -> Maybe UTCTime
formatTime caps_time_locale fmt t
where
upper_suffix = map toUpper (date_suffix t)
- fmt = "%A, %B %d" ++ upper_suffix ++ " (" ++ date_format_padded ++ ")"
+ 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