From 700d15a5ff22cea8beceef9e81f7764f87976088 Mon Sep 17 00:00:00 2001 From: "Julian R. Smith" Date: Thu, 15 Apr 2021 16:49:11 -0500 Subject: [PATCH] Fixes #3 - hack to fix date timezone parsing after export changed --- tastypl.go | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/tastypl.go b/tastypl.go index a08adf4..8e6e8f1 100644 --- a/tastypl.go +++ b/tastypl.go @@ -376,7 +376,11 @@ func parseDecimal(value string) decimal.Decimal { return d } +// More ugliness. TW changed export formats in Apri 2021 (at least). Exporting timezone identifiers instead of numeric is problematic. +// What variant of CST is it? Central (Chicago)? China? Cuba? And The local system will decide, even worse. const almostRFC3339 = "2006-01-02T15:04:05-0700" +const anotherAlmostRFC3339 = "2006-01-02T15:04:05.000CDT" +const andAnotherAlmostRFC3339 = "2006-01-02T15:04:05.000CST" type position struct { // Opening transaction(s) for this position @@ -622,6 +626,12 @@ func (p *portfolio) parseTransactions(records [][]string, nofutures bool) { func (p *portfolio) parseTransaction(i int, rec []string, ytd *bool) *transaction { date, err := time.Parse(almostRFC3339, rec[0]) + if err != nil { + date, err = time.Parse(anotherAlmostRFC3339, rec[0]) + } + if err != nil { + date, err = time.Parse(andAnotherAlmostRFC3339, rec[0]) + } if err != nil { glog.Fatalf("record #%d, bad transaction date: %s", i, err) } @@ -1533,6 +1543,12 @@ func dumpChart(records [][]string, ytd, nofutures, ignoreacat bool, nocash bool) portfolio.AddTransaction(record) //portfolio.PrintStats() date, err := time.Parse(almostRFC3339, record[0]) + if err != nil { + date, err = time.Parse(anotherAlmostRFC3339, record[0]) + } + if err != nil { + date, err = time.Parse(andAnotherAlmostRFC3339, record[0]) + } if err != nil { glog.Fatalf("record #%d, bad transaction date: %s", len(portfolio.transactions), err) } @@ -1541,6 +1557,12 @@ func dumpChart(records [][]string, ytd, nofutures, ignoreacat bool, nocash bool) // time change so that we have only one data point per timestamp. if i != len(records)-1 { nextDate, err := time.Parse(almostRFC3339, records[i+1][0]) + if err != nil { + date, err = time.Parse(anotherAlmostRFC3339, records[i+1][0]) + } + if err != nil { + date, err = time.Parse(andAnotherAlmostRFC3339, records[i+1][0]) + } if err == nil && date.Equal(nextDate) { continue } @@ -1696,7 +1718,12 @@ func dumpDaily(records [][]string, ytd, nofutures, ignoreacat bool, exportTD boo for i, record := range records { portfolio.AddTransaction(record) date, err := time.Parse(almostRFC3339, record[0]) - + if err != nil { + date, err = time.Parse(anotherAlmostRFC3339, record[0]) + } + if err != nil { + date, err = time.Parse(andAnotherAlmostRFC3339, record[0]) + } if err != nil { glog.Fatalf("record #%d, bad transaction date: %s", len(portfolio.transactions), err) } @@ -1704,6 +1731,12 @@ func dumpDaily(records [][]string, ytd, nofutures, ignoreacat bool, exportTD boo // date change so that we have only one data point per day. if i != len(records)-1 { nextDate, err := time.Parse(almostRFC3339, records[i+1][0]) + if err != nil { + nextDate, err = time.Parse(anotherAlmostRFC3339, records[i+1][0]) + } + if err != nil { + nextDate, err = time.Parse(andAnotherAlmostRFC3339, records[i+1][0]) + } if err == nil && date.YearDay() == nextDate.YearDay() { continue }