From 7dcfcb05ecb07f7b4a9aced4bbd48aaefe44fa1d Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Fri, 30 Dec 2016 11:44:01 -0800 Subject: [PATCH] lib: get effective report start/end dates --- hledger-lib/Hledger/Reports/ReportOptions.hs | 30 ++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/hledger-lib/Hledger/Reports/ReportOptions.hs b/hledger-lib/Hledger/Reports/ReportOptions.hs index 8c51f7c0e..de6b10737 100644 --- a/hledger-lib/Hledger/Reports/ReportOptions.hs +++ b/hledger-lib/Hledger/Reports/ReportOptions.hs @@ -22,6 +22,9 @@ module Hledger.Reports.ReportOptions ( queryOptsFromOpts, transactionDateFn, postingDateFn, + reportStartDate, + reportEndDate, + reportStartEndDates, tests_Hledger_Reports_ReportOptions ) @@ -354,6 +357,33 @@ tests_queryOptsFromOpts = [ }) ] +-- | The effective report start date is the one specified by options or queries, +-- otherwise the earliest transaction or posting date in the journal, +-- otherwise (for an empty journal) nothing. +-- Needs IO to parse smart dates in options/queries. +reportStartDate :: Journal -> ReportOpts -> IO (Maybe Day) +reportStartDate j ropts = (fst <$>) <$> reportStartEndDates j ropts + +-- | The effective report end date is the one specified by options or queries, +-- otherwise the latest transaction or posting date in the journal, +-- otherwise (for an empty journal) nothing. +-- Needs IO to parse smart dates in options/queries. +reportEndDate :: Journal -> ReportOpts -> IO (Maybe Day) +reportEndDate j ropts = (fst <$>) <$> reportStartEndDates j ropts + +reportStartEndDates :: Journal -> ReportOpts -> IO (Maybe (Day,Day)) +reportStartEndDates j ropts = do + today <- getCurrentDay + let + q = queryFromOpts today ropts + mrequestedstartdate = queryStartDate False q + mrequestedenddate = queryEndDate False q + return $ + case journalDateSpan False j of -- don't bother with secondary dates + DateSpan (Just journalstartdate) (Just journalenddate) -> + Just (fromMaybe journalstartdate mrequestedstartdate, fromMaybe journalenddate mrequestedenddate) + _ -> Nothing + tests_Hledger_Reports_ReportOptions :: Test tests_Hledger_Reports_ReportOptions = TestList $