Skip to content

Commit

Permalink
Fixes #3 - hack to fix date timezone parsing after export changed
Browse files Browse the repository at this point in the history
  • Loading branch information
elescondite committed Apr 15, 2021
1 parent 6645fd9 commit 700d15a
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion tastypl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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
}
Expand Down Expand Up @@ -1696,14 +1718,25 @@ 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)
}
// keep processing transactions until we have a
// 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
}
Expand Down

0 comments on commit 700d15a

Please sign in to comment.