parsing: use actual (ie, first) date's year as a default for the effective (ie, second) date

This commit is contained in:
Simon Michael 2010-02-03 21:19:01 +00:00
parent 86020e8f4a
commit c08bccb158
2 changed files with 12 additions and 6 deletions

View File

@ -310,7 +310,7 @@ ledgerDefaultYear = do
ledgerTransaction :: GenParser Char LedgerFileCtx Transaction ledgerTransaction :: GenParser Char LedgerFileCtx Transaction
ledgerTransaction = do ledgerTransaction = do
date <- ledgerdate <?> "transaction" date <- ledgerdate <?> "transaction"
edate <- try (ledgereffectivedate <?> "effective date") <|> return Nothing edate <- try (ledgereffectivedate date <?> "effective date") <|> return Nothing
status <- ledgerstatus status <- ledgerstatus
code <- ledgercode code <- ledgercode
description <- many1 spacenonewline >> liftM rstrip (many1 (noneOf ";\n") <?> "description") 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) let tod = TimeOfDay (read h) (read m) (maybe 0 (fromIntegral.read) s)
return $ LocalTime day tod return $ LocalTime day tod
ledgereffectivedate :: GenParser Char LedgerFileCtx (Maybe Day) ledgereffectivedate :: Day -> GenParser Char LedgerFileCtx (Maybe Day)
ledgereffectivedate = do ledgereffectivedate actualdate = do
char '=' 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 return $ Just edate
ledgerstatus :: GenParser Char st Bool ledgerstatus :: GenParser Char st Bool

View File

@ -1,8 +1,6 @@
# #
-f - print --effective -f - print --effective
<<< <<<
Y 2009
2009/1/1=1/2 x 2009/1/1=1/2 x
a 1 a 1
b b
@ -11,3 +9,4 @@ Y 2009
a 1 a 1
b -1 b -1
>>>2