From 7acb5d45aafa6e22cfaea49a0ebce5ab10b21758 Mon Sep 17 00:00:00 2001 From: Dmitry Astapov Date: Sat, 25 Nov 2017 00:42:39 +0000 Subject: [PATCH] lib: make month names in period expressions case-insensitive Currently only lower-case account names are supported, both on the command line, and in the journal (in periodic transactions): This works: $ hledger balance -p nov This does not: $ hledger balance -p Nov First transaction will parse, second will not: ``` cat every-month.journal ~/devel/haskell/darcs-get/hledger/examples ~ aug to sep assets expenses $1 ~ Aug to Sep assets expenses $2 ``` $../bin/hledger-budget bal -f every-month.journal hledger-budget: Failed to parse "Aug to Sep": date parse error () This commit fixes both cases. --- hledger-lib/Hledger/Data/Dates.hs | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/hledger-lib/Hledger/Data/Dates.hs b/hledger-lib/Hledger/Data/Dates.hs index ab8590935..c454afd28 100644 --- a/hledger-lib/Hledger/Data/Dates.hs +++ b/hledger-lib/Hledger/Data/Dates.hs @@ -257,7 +257,7 @@ earliest (Just d1) (Just d2) = Just $ min d1 d2 -- | Parse a period expression to an Interval and overall DateSpan using -- the provided reference date, or return a parse error. parsePeriodExpr :: Day -> Text -> Either (ParseError Char MPErr) (Interval, DateSpan) -parsePeriodExpr refdate = parsewith (periodexpr refdate <* eof) +parsePeriodExpr refdate s = parsewith (periodexpr refdate <* eof) (T.toLower s) maybePeriod :: Day -> Text -> Maybe (Interval,DateSpan) maybePeriod refdate = either (const Nothing) Just . parsePeriodExpr refdate @@ -671,14 +671,8 @@ monthabbrevs = ["jan","feb","mar","apr","may","jun","jul","aug","sep","oct","n -- weekdays = ["monday","tuesday","wednesday","thursday","friday","saturday","sunday"] -- weekdayabbrevs = ["mon","tue","wed","thu","fri","sat","sun"] -#if MIN_VERSION_megaparsec(6,0,0) -lc = T.toLower -#else -lc = lowercase -#endif - -monthIndex t = maybe 0 (+1) $ lc t `elemIndex` months -monIndex t = maybe 0 (+1) $ lc t `elemIndex` monthabbrevs +monthIndex t = maybe 0 (+1) $ t `elemIndex` months +monIndex t = maybe 0 (+1) $ t `elemIndex` monthabbrevs month :: SimpleTextParser SmartDate month = do @@ -718,12 +712,12 @@ lastthisnextthing = do return ("", T.unpack r, T.unpack p) -- | --- >>> let p = parsewith (periodexpr (parsedate "2008/11/26")) :: T.Text -> Either (ParseError Char MPErr) (Interval, DateSpan) --- >>> p "from aug to oct" +-- >>> let p s = parsewith (periodexpr (parsedate "2008/11/26")) (T.toLower s) :: Either (ParseError Char MPErr) (Interval, DateSpan) +-- >>> p "from Aug to Oct" -- Right (NoInterval,DateSpan 2008/08/01-2008/09/30) -- >>> p "aug to oct" -- Right (NoInterval,DateSpan 2008/08/01-2008/09/30) --- >>> p "every 3 days in aug" +-- >>> p "every 3 days in Aug" -- Right (Days 3,DateSpan 2008/08) -- >>> p "daily from aug" -- Right (Days 1,DateSpan 2008/08/01-)