register: support date:/date2:/--date2 better (fix #201, #221, #222)

This commit is contained in:
Simon Michael 2014-12-24 16:11:30 -08:00
parent 0142482834
commit c138c35d3b
5 changed files with 95 additions and 56 deletions

View File

@ -19,11 +19,14 @@ module Hledger.Query (
queryIsAcct,
queryIsDepth,
queryIsDate,
queryIsDate2,
queryIsDateOrDate2,
queryIsStartDateOnly,
queryIsSym,
queryStartDate,
queryEndDate,
queryDateSpan,
queryDateSpan',
queryDepth,
queryEmpty,
inAccount,
@ -415,9 +418,17 @@ queryIsDepth _ = False
queryIsDate :: Query -> Bool
queryIsDate (Date _) = True
queryIsDate (Date2 _) = True
queryIsDate _ = False
queryIsDate2 :: Query -> Bool
queryIsDate2 (Date2 _) = True
queryIsDate2 _ = False
queryIsDateOrDate2 :: Query -> Bool
queryIsDateOrDate2 (Date _) = True
queryIsDateOrDate2 (Date2 _) = True
queryIsDateOrDate2 _ = False
queryIsDesc :: Query -> Bool
queryIsDesc (Desc _) = True
queryIsDesc _ = False
@ -477,6 +488,20 @@ queryDateSpans False (Date span) = [span]
queryDateSpans True (Date2 span) = [span]
queryDateSpans _ _ = []
-- | What date span (or secondary date span) does this query specify ?
-- For OR expressions, use the widest possible span. NOT is ignored.
queryDateSpan' :: Query -> DateSpan
queryDateSpan' q = spansUnion $ queryDateSpans' q
-- | Extract all date (or secondary date) spans specified in this query.
-- NOT is ignored.
queryDateSpans' :: Query -> [DateSpan]
queryDateSpans' (Or qs) = concatMap queryDateSpans' qs
queryDateSpans' (And qs) = concatMap queryDateSpans' qs
queryDateSpans' (Date span) = [span]
queryDateSpans' (Date2 span) = [span]
queryDateSpans' _ = []
-- | What is the earliest of these dates, where Nothing is latest ?
earliestMaybeDate :: [Maybe Day] -> Maybe Day
earliestMaybeDate mds = head $ sortBy compareMaybeDates mds ++ [Nothing]

View File

@ -71,7 +71,7 @@ multiBalanceReport opts q j = MultiBalanceReport (displayspans, items, totals)
depthq = dbg "depthq" $ filterQuery queryIsDepth q
depth = queryDepth depthq
depthless = dbg "depthless" . filterQuery (not . queryIsDepth)
datelessq = dbg "datelessq" $ filterQuery (not . queryIsDate) q
datelessq = dbg "datelessq" $ filterQuery (not . queryIsDateOrDate2) q
dateqcons = if date2_ opts then Date2 else Date
precedingq = dbg "precedingq" $ And [datelessq, dateqcons $ DateSpan Nothing (spanStart reportspan)]
requestedspan = dbg "requestedspan" $ queryDateSpan (date2_ opts) q -- span specified by -b/-e/-p options and query args

View File

@ -57,12 +57,13 @@ postingsReport opts q j = (totallabel, items)
symq = dbg "symq" $ filterQuery queryIsSym $ dbg "requested q" q
depth = queryDepth q
depthless = filterQuery (not . queryIsDepth)
datelessq = filterQuery (not . queryIsDate) q
(dateqcons,pdate) | date2_ opts = (Date2, postingDate2)
| otherwise = (Date, postingDate)
requestedspan = dbg "requestedspan" $ queryDateSpan (date2_ opts) q -- span specified by -b/-e/-p options and query args
-- XXX doesn't handle date2:
requestedspan' = dbg "requestedspan'" $ requestedspan `spanDefaultsFrom` journalDateSpan (date2_ opts) j -- if open-ended, close it using the journal's end dates
datelessq = filterQuery (not . queryIsDateOrDate2) q
-- XXX date:/date2:/--date2 handling is not robust, combinations of these can confuse it
dateq = filterQuery queryIsDateOrDate2 q
(dateqcons,pdate) | queryIsDate2 dateq || (queryIsDate dateq && date2_ opts) = (Date2, postingDate2)
| otherwise = (Date, postingDate)
requestedspan = dbg "requestedspan" $ queryDateSpan' q -- span specified by -b/-e/-p options and query args
requestedspan' = dbg "requestedspan'" $ requestedspan `spanDefaultsFrom` journalDateSpan ({-date2_ opts-} False) j -- if open-ended, close it using the journal's end dates
intervalspans = dbg "intervalspans" $ splitSpan (intervalFromOpts opts) requestedspan' -- interval spans enclosing it
reportstart = dbg "reportstart" $ maybe Nothing spanStart $ headMay intervalspans
reportend = dbg "reportend" $ maybe Nothing spanEnd $ lastMay intervalspans

View File

@ -1,48 +0,0 @@
# filtering by secondary date (cf #201)
# 1. -b/-e/-p match the secondary date if --date2 is present
hledgerdev -f- register -p 2014/1/2 --date2
<<<
2014/1/1=1/2
(a) 1
2014/1/3
(b) 1
>>>
2014/01/02 (a) 1 1
>>>=0
# 2. date: is not affected by --date2
hledgerdev -f- register date:2014/1/2 --date2
<<<
2014/1/1=1/2
(a) 1
2014/1/3
(b) 1
>>>
>>>=0
# 3. date2: matches the secondary date
hledgerdev -f- register date2:2014/1/2
<<<
2014/1/1=1/2
(a) 1
2014/1/3
(b) 1
>>>
2014/01/02 (a) 1 1
>>>=0
# 4. date2: is not affected by --date2
hledgerdev -f- register date2:2014/1/1 --date2
<<<
2014/1/1=1/2
(a) 1
2014/1/3
(b) 1
>>>
>>>=0

View File

@ -1,3 +1,4 @@
# 1.
hledgerdev -f - register --date2
<<<
2009/1/1=2010/1/1 x
@ -7,3 +8,63 @@ hledgerdev -f - register --date2
2010/01/01 x a 1 1
b -1 0
>>>=0
# 2. -b/-e/-p match the secondary date if --date2 is present (also, the secondary date is displayed)
hledgerdev -f- register -p 2014/1/2 --date2
<<<
2014/1/1=1/2
(a) 1
2014/1/3
(b) 1
>>>
2014/01/02 (a) 1 1
>>>=0
# 3. date: matches the secondary date if --date2 is present (on the command line, at least)
hledgerdev -f- register date:2014/1/2 --date2
<<<
2014/1/1=1/2
(a) 1
2014/1/3
(b) 1
>>>
2014/01/02 (a) 1 1
>>>=0
# 4. date2: matches the secondary date
hledgerdev -f- register date2:2014/1/2
<<<
2014/1/1=1/2
(a) 1
2014/1/3
(b) 1
>>>
2014/01/01 (a) 1 1
>>>=0
# 5. you need to add --date2 if you also want the secondary date displayed
hledgerdev -f- register date2:2014/1/2 --date2
<<<
2014/1/1=1/2
(a) 1
2014/1/3
(b) 1
>>>
2014/01/02 (a) 1 1
>>>=0
# 3. date2: matching is not affected by --date2
hledgerdev -f- register date2:2014/1/1 --date2
<<<
2014/1/1=1/2
(a) 1
2014/1/3
(b) 1
>>>
>>>=0