From c08bccb15883fecdbb5c7443824ae93b7e8c15b6 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Wed, 3 Feb 2010 21:19:01 +0000 Subject: [PATCH] parsing: use actual (ie, first) date's year as a default for the effective (ie, second) date --- Ledger/Parse.hs | 15 +++++++++++---- tests/{effective-day.test => effective-date.test} | 3 +-- 2 files changed, 12 insertions(+), 6 deletions(-) rename tests/{effective-day.test => effective-date.test} (93%) diff --git a/Ledger/Parse.hs b/Ledger/Parse.hs index 35cac2e76..aff8f059d 100644 --- a/Ledger/Parse.hs +++ b/Ledger/Parse.hs @@ -310,7 +310,7 @@ ledgerDefaultYear = do ledgerTransaction :: GenParser Char LedgerFileCtx Transaction ledgerTransaction = do date <- ledgerdate "transaction" - edate <- try (ledgereffectivedate "effective date") <|> return Nothing + edate <- try (ledgereffectivedate date "effective date") <|> return Nothing status <- ledgerstatus code <- ledgercode description <- many1 spacenonewline >> liftM rstrip (many1 (noneOf ";\n") "description") @@ -352,10 +352,17 @@ ledgerdatetime = do let tod = TimeOfDay (read h) (read m) (maybe 0 (fromIntegral.read) s) return $ LocalTime day tod -ledgereffectivedate :: GenParser Char LedgerFileCtx (Maybe Day) -ledgereffectivedate = do +ledgereffectivedate :: Day -> GenParser Char LedgerFileCtx (Maybe Day) +ledgereffectivedate actualdate = do char '=' - edate <- ledgerdate + -- kludgily use actual date for default year + let withDefaultYear d p = do + y <- getYear + let (y',_,_) = toGregorian d in setYear y' + r <- p + when (isJust y) $ setYear $ fromJust y + return r + edate <- withDefaultYear actualdate ledgerdate return $ Just edate ledgerstatus :: GenParser Char st Bool diff --git a/tests/effective-day.test b/tests/effective-date.test similarity index 93% rename from tests/effective-day.test rename to tests/effective-date.test index 7f7cf5529..91134b263 100644 --- a/tests/effective-day.test +++ b/tests/effective-date.test @@ -1,8 +1,6 @@ # -f - print --effective <<< -Y 2009 - 2009/1/1=1/2 x a 1 b @@ -11,3 +9,4 @@ Y 2009 a 1 b -1 +>>>2