From bbe3be44e20cfaf9941cc690192d0fd1c11cf561 Mon Sep 17 00:00:00 2001 From: Alex Date: Mon, 20 Nov 2023 16:37:42 +0100 Subject: [PATCH] fixed date comparison issue --- eval.go | 22 ++++++++-------------- eval_test.go | 2 +- 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/eval.go b/eval.go index 98ee150..5401b76 100644 --- a/eval.go +++ b/eval.go @@ -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 { @@ -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 } @@ -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} @@ -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} @@ -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} @@ -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} diff --git a/eval_test.go b/eval_test.go index bb9ad4f..1ad45ae 100644 --- a/eval_test.go +++ b/eval_test.go @@ -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") } }