From 1ae357a9a52ed815863233db8bd15f4488db9c3f Mon Sep 17 00:00:00 2001 From: Chris Howey Date: Sat, 28 May 2022 07:54:00 -0500 Subject: [PATCH] fix: have start/end dates be inclusive for command Fixes #34 --- date.go | 3 +++ ledger/cmd/print.go | 18 ++++-------------- ledger/cmd/printAccounts.go | 2 +- ledger/cmd/printBalance.go | 2 +- ledger/cmd/printEquity.go | 2 +- 5 files changed, 10 insertions(+), 17 deletions(-) diff --git a/date.go b/date.go index b286cb6..f999528 100644 --- a/date.go +++ b/date.go @@ -129,6 +129,9 @@ func startEndTime(trans []*Transaction) (start, end time.Time) { } } + // to include last days' transactions in date period splits + end = end.Add(time.Second) + return } diff --git a/ledger/cmd/print.go b/ledger/cmd/print.go index ac26466..60b8e7e 100644 --- a/ledger/cmd/print.go +++ b/ledger/cmd/print.go @@ -39,6 +39,9 @@ func cliTransactions() ([]*ledger.Transaction, error) { return nil, errors.New("unable to parse start or end date string argument") } + // include end dates' transactions too + parsedEndDate = parsedEndDate.Add(time.Second) + var generalLedger []*ledger.Transaction var parseError error if ledgerFilePath == "-" { @@ -54,20 +57,7 @@ func cliTransactions() ([]*ledger.Transaction, error) { return generalLedger[i].Date.Before(generalLedger[j].Date) }) - timeStartIndex, timeEndIndex := 0, 0 - for idx := 0; idx < len(generalLedger); idx++ { - if generalLedger[idx].Date.After(parsedStartDate) { - timeStartIndex = idx - break - } - } - for idx := len(generalLedger) - 1; idx >= 0; idx-- { - if generalLedger[idx].Date.Before(parsedEndDate) { - timeEndIndex = idx - break - } - } - generalLedger = generalLedger[timeStartIndex : timeEndIndex+1] + generalLedger = ledger.TransactionsInDateRange(generalLedger, parsedStartDate, parsedEndDate) origLedger := generalLedger generalLedger = make([]*ledger.Transaction, 0) diff --git a/ledger/cmd/printAccounts.go b/ledger/cmd/printAccounts.go index 1572710..184fede 100644 --- a/ledger/cmd/printAccounts.go +++ b/ledger/cmd/printAccounts.go @@ -60,7 +60,7 @@ func init() { var startDate, endDate time.Time startDate = time.Date(1970, 1, 1, 0, 0, 0, 0, time.Local) - endDate = time.Now().Add(time.Hour * 24) + endDate = time.Now() accountsCmd.Flags().StringVarP(&startString, "begin-date", "b", startDate.Format(transactionDateFormat), "Begin date of transaction processing.") accountsCmd.Flags().StringVarP(&endString, "end-date", "e", endDate.Format(transactionDateFormat), "End date of transaction processing.") accountsCmd.Flags().BoolVarP(&accountLeavesOnly, "leaves-only", "l", false, "Only show most-depth accounts") diff --git a/ledger/cmd/printBalance.go b/ledger/cmd/printBalance.go index b8a42f3..9c26cc7 100644 --- a/ledger/cmd/printBalance.go +++ b/ledger/cmd/printBalance.go @@ -43,7 +43,7 @@ func init() { var startDate, endDate time.Time startDate = time.Date(1970, 1, 1, 0, 0, 0, 0, time.Local) - endDate = time.Now().Add(time.Hour * 24) + endDate = time.Now() balanceCmd.Flags().StringVarP(&startString, "begin-date", "b", startDate.Format(transactionDateFormat), "Begin date of transaction processing.") balanceCmd.Flags().StringVarP(&endString, "end-date", "e", endDate.Format(transactionDateFormat), "End date of transaction processing.") balanceCmd.Flags().StringVar(&payeeFilter, "payee", "", "Filter output to payees that contain this string.") diff --git a/ledger/cmd/printEquity.go b/ledger/cmd/printEquity.go index 0919adc..97ab290 100644 --- a/ledger/cmd/printEquity.go +++ b/ledger/cmd/printEquity.go @@ -60,7 +60,7 @@ func init() { var startDate, endDate time.Time startDate = time.Date(1970, 1, 1, 0, 0, 0, 0, time.Local) - endDate = time.Now().Add(time.Hour * 24) + endDate = time.Now() equityCmd.Flags().StringVarP(&startString, "begin-date", "b", startDate.Format(transactionDateFormat), "Begin date of transaction processing.") equityCmd.Flags().StringVarP(&endString, "end-date", "e", endDate.Format(transactionDateFormat), "End date of transaction processing.") } -- 2.45.2