Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix type comparisons for Nullsafe* functions #13605

Merged
merged 8 commits into from
Jul 26, 2023
Prev Previous commit
Next Next commit
datetime: Fix parsing integers into datetime
A 0 time is still valid.

Signed-off-by: Dirkjan Bussink <d.bussink@gmail.com>
dbussink committed Jul 24, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit a736f831165f2bcc32059a2420ea7aefbdca9fcf
2 changes: 1 addition & 1 deletion go/mysql/datetime/parse.go
Original file line number Diff line number Diff line change
@@ -321,7 +321,7 @@ func ParseDateTimeInt64(i int64) (dt DateTime, ok bool) {
if i == 0 {
return dt, true
}
if t == 0 || d == 0 {
if d == 0 {
return dt, false
}
dt.Time, ok = ParseTimeInt64(t)
51 changes: 51 additions & 0 deletions go/mysql/datetime/parse_test.go
Original file line number Diff line number Diff line change
@@ -17,6 +17,7 @@ limitations under the License.
package datetime

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
@@ -290,3 +291,53 @@ func TestParseDateTime(t *testing.T) {
})
}
}

func TestParseDateTimeInt64(t *testing.T) {
type datetime struct {
year int
month int
day int
hour int
minute int
second int
nanosecond int
}
tests := []struct {
input int64
output datetime
l int
err bool
}{
{input: 1, output: datetime{}, err: true},
{input: 20221012000000, output: datetime{2022, 10, 12, 0, 0, 0, 0}},
{input: 20221012112233, output: datetime{2022, 10, 12, 11, 22, 33, 0}},
}

for _, test := range tests {
t.Run(fmt.Sprintf("%d", test.input), func(t *testing.T) {
got, ok := ParseDateTimeInt64(test.input)
if test.err {
if !got.IsZero() {
assert.Equal(t, test.output.year, got.Date.Year())
assert.Equal(t, test.output.month, got.Date.Month())
assert.Equal(t, test.output.day, got.Date.Day())
assert.Equal(t, test.output.hour, got.Time.Hour())
assert.Equal(t, test.output.minute, got.Time.Minute())
assert.Equal(t, test.output.second, got.Time.Second())
assert.Equal(t, test.output.nanosecond, got.Time.Nanosecond())
}
assert.Falsef(t, ok, "did not fail to parse %s", test.input)
return
}

require.True(t, ok)
assert.Equal(t, test.output.year, got.Date.Year())
assert.Equal(t, test.output.month, got.Date.Month())
assert.Equal(t, test.output.day, got.Date.Day())
assert.Equal(t, test.output.hour, got.Time.Hour())
assert.Equal(t, test.output.minute, got.Time.Minute())
assert.Equal(t, test.output.second, got.Time.Second())
assert.Equal(t, test.output.nanosecond, got.Time.Nanosecond())
})
}
}