Skip to content

Commit

Permalink
fixed date comparison issue
Browse files Browse the repository at this point in the history
  • Loading branch information
ax4w committed Nov 20, 2023
1 parent f730cd0 commit bbe3be4
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 15 deletions.
22 changes: 8 additions & 14 deletions eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,9 @@ func convertBytesToFloat(v []byte) float64 {
return r
}

// -2 d1 is greater
// -1 d1 is greater than
// -1 d1 is greater
// 0 equal
// 1 d2 is greater than
// 2 d2 is greater
// 1 d2 is greater
func compareDates(d1, d2 string) int {
t1, err := time.Parse("2006-01-02", d1)
if err != nil {
Expand All @@ -109,15 +107,11 @@ func compareDates(d1, d2 string) int {
td2 := t2.Unix()
switch {
case td1 > td2:
return -2
return -1
case td1 == td2:
return 0
case td1 >= td2:
return -1
case td2 >= td1:
return 1
case td2 > td1:
return 2
return 1
}
return 0
}
Expand Down Expand Up @@ -148,7 +142,7 @@ func evaluate(f *token) *token {
compareCheck(r, l)
if l.tokenType == tokenTypeDate && r.tokenType == tokenTypeDate {
i := compareDates(string(l.value), string(r.value))
if i == -2 {
if i == 1 {
return &token{value: []byte("t"), tokenType: tokenTypeBoolean}
}
return &token{value: []byte("f"), tokenType: tokenTypeBoolean}
Expand All @@ -164,7 +158,7 @@ func evaluate(f *token) *token {
compareCheck(r, l)
if l.tokenType == tokenTypeDate && r.tokenType == tokenTypeDate {
i := compareDates(string(l.value), string(r.value))
if i == 2 {
if i == -1 {
return &token{value: []byte("t"), tokenType: tokenTypeBoolean}
}
return &token{value: []byte("f"), tokenType: tokenTypeBoolean}
Expand All @@ -180,7 +174,7 @@ func evaluate(f *token) *token {
compareCheck(r, l)
if l.tokenType == tokenTypeDate && r.tokenType == tokenTypeDate {
i := compareDates(string(l.value), string(r.value))
if i == 1 {
if i == -1 || i == 0 {
return &token{value: []byte("t"), tokenType: tokenTypeBoolean}
}
return &token{value: []byte("f"), tokenType: tokenTypeBoolean}
Expand All @@ -196,7 +190,7 @@ func evaluate(f *token) *token {
compareCheck(r, l)
if l.tokenType == tokenTypeDate && r.tokenType == tokenTypeDate {
i := compareDates(string(l.value), string(r.value))
if i == -1 {
if i == 1 || i == 0 {
return &token{value: []byte("t"), tokenType: tokenTypeBoolean}
}
return &token{value: []byte("f"), tokenType: tokenTypeBoolean}
Expand Down
2 changes: 1 addition & 1 deletion eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestEval(t *testing.T) {
if !eval("2023-11-19 == 2023-11-19") {
t.Fatalf("Should be true")
}
if !eval("2023-11-19 == 2023-11-19") {
if !eval("2022-11-19 <= 2023-11-19") {
t.Fatalf("Should be true")
}
}

0 comments on commit bbe3be4

Please sign in to comment.