Adding optional start and end dates for extrapolation

This commit is contained in:
2022-08-27 20:10:00 -04:00
parent 8864c83998
commit b78466d32f
4 changed files with 66 additions and 8 deletions

37
main.go
View File

@@ -10,6 +10,8 @@ import (
"strings"
"time"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
)
@@ -39,6 +41,8 @@ type Config struct {
}
type ConfigYaml struct {
Start string
End string
Accounts []Account
Credit []TransactionYaml
Transactions []TransactionYaml
@@ -118,7 +122,7 @@ func parseDate(date string) (time.Time, bool) {
if date == "first" {
formalDate = current
} else if date == "last" {
formalDate = current.AddDate(0, 1, -current.Day())
formalDate = lastOfMonth(current)
} else {
dateString = fmt.Sprintf("%d-%d-%s",
time.Now().Year(), int(time.Now().Month()), date)
@@ -219,6 +223,14 @@ func executeTransaction(transactionLog *TransactionLog, accounts []Account, tran
return executed
}
func firstOfMonth(t time.Time) time.Time {
return time.Date(t.Year(), t.Month(), 1, 0, 0, 0, 0, time.UTC)
}
func lastOfMonth(t time.Time) time.Time {
return time.Date(t.Year(), t.Month()+1, 0, 0, 0, 0, 0, time.UTC)
}
func calculateFrequency(frequency string, startDate time.Time, endDate time.Time, hardDate bool) []time.Time {
base := time.Now()
current := time.Date(base.Year(),
@@ -237,7 +249,11 @@ func calculateFrequency(frequency string, startDate time.Time, endDate time.Time
} else if frequency == "biweekly" {
initDate = initDate.AddDate(0, 0, 14)
} else if frequency == "monthly" {
initDate = initDate.AddDate(0, 1, 0)
if initDate == lastOfMonth(initDate) {
initDate = lastOfMonth(firstOfMonth(initDate).AddDate(0, 1, 0))
} else {
initDate = initDate.AddDate(0, 1, 0)
}
} else {
break
}
@@ -260,7 +276,7 @@ func printOutput(date time.Time, initialBalances []Account, executedCharges []Tr
divider2 := delim2 + "|"
fmt.Println(divider)
output := fmt.Sprintf(date.Format("Jan 02 2006"))
output := fmt.Sprint(date.Format("Jan 02 2006"))
rear_spacer := strings.Repeat(" ", 65-len(output))
fmt.Printf("%s%s |\n", output, rear_spacer)
for _, v := range initialBalances {
@@ -325,6 +341,21 @@ func main() {
config := parseConfig(&configYaml)
preFetchDates(config.Credit, &transactionLog)
preFetchDates(config.Transactions, &transactionLog)
if configYaml.Start != "" {
transactionLog.StartDate, err = time.Parse("2006-1-2", configYaml.Start)
if err != nil {
logrus.Error(err)
}
}
if configYaml.End != "" {
transactionLog.EndDate, err = time.Parse("2006-1-2", configYaml.End)
if err != nil {
logrus.Error(err)
}
}
collectTransactions(config.Credit, &transactionLog)
collectTransactions(config.Transactions, &transactionLog)