Skip to content

Commit

Permalink
Convert FII dividends date to YYYY-MM-DD format
Browse files Browse the repository at this point in the history
  • Loading branch information
dude333 committed Apr 26, 2021
1 parent 6625ab2 commit 9d027ae
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 2 deletions.
13 changes: 11 additions & 2 deletions parsers/fiidb.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ func (fii FIIStore) StoreFIIDividends(stream map[string]string) error {
}

code := mapFinder("Código de negociação da cota", stream)
baseDate := mapFinder("Data-base", stream)
pymtDate := mapFinder("Data do pagamento", stream)
baseDate := fixDate(mapFinder("Data-base", stream))
pymtDate := fixDate(mapFinder("Data do pagamento", stream))
val := mapFinder("Valor do provento por cota", stream)

const insert = `INSERT OR IGNORE INTO fii_dividends
Expand Down Expand Up @@ -167,3 +167,12 @@ func comma2dot(val string) float64 {
n, _ := strconv.ParseFloat(b, 64)
return n
}

// fixDate converts dates from DD/MM/YYYY to YYYY-MM-DD.
func fixDate(date string) string {
if len(date) != len("26/04/2021") || strings.Count(date, "/") != 2 {
return date
}

return date[6:10] + "-" + date[3:5] + "-" + date[0:2]
}
63 changes: 63 additions & 0 deletions parsers/fiidb_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package parsers

import (
"testing"
)

func Test_comma2dot(t *testing.T) {
type args struct {
val string
}
tests := []struct {
name string
args args
want float64
}{
{
name: "should work",
args: args{val: "1.230,56"},
want: 1230.56,
},
{
name: "should return 0",
args: args{val: "shouldbeanum"},
want: 0,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := comma2dot(tt.args.val); got != tt.want {
t.Errorf("comma2dot() = %v, want %v", got, tt.want)
}
})
}
}

func Test_convertDateStr(t *testing.T) {
type args struct {
date string
}
tests := []struct {
name string
args args
want string
}{
{
name: "should work",
args: args{date: "01/02/2021"},
want: "2021-02-01",
},
{
name: "should return the input",
args: args{date: "wrong/date"},
want: "wrong/date",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := fixDate(tt.args.date); got != tt.want {
t.Errorf("fixDate() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 9d027ae

Please sign in to comment.