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 parsing 0 year. #1257

Merged
merged 5 commits into from Nov 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 0 additions & 10 deletions utils.go
Expand Up @@ -118,10 +118,6 @@ func parseDateTime(b []byte, loc *time.Location) (time.Time, error) {
if err != nil {
return time.Time{}, err
}
if year <= 0 {
year = 1
}

if b[4] != '-' {
return time.Time{}, fmt.Errorf("bad value for field: `%c`", b[4])
}
Expand All @@ -130,9 +126,6 @@ func parseDateTime(b []byte, loc *time.Location) (time.Time, error) {
if err != nil {
return time.Time{}, err
}
if m <= 0 {
m = 1
}
month := time.Month(m)

if b[7] != '-' {
Expand All @@ -143,9 +136,6 @@ func parseDateTime(b []byte, loc *time.Location) (time.Time, error) {
if err != nil {
return time.Time{}, err
}
if day <= 0 {
day = 1
}
if len(b) == 10 {
return time.Date(year, month, day, 0, 0, 0, 0, loc), nil
}
Expand Down
27 changes: 27 additions & 0 deletions utils_test.go
Expand Up @@ -440,6 +440,33 @@ func TestParseDateTime(t *testing.T) {
}
}

func TestInvalidDateTime(t *testing.T) {
cases := []struct {
name string
str string
want time.Time
}{
{
name: "parse datetime without day",
str: "0000-00-00 21:30:45",
want: time.Date(0, 0, 0, 21, 30, 45, 0, time.UTC),
},
}

for _, cc := range cases {
t.Run(cc.name, func(t *testing.T) {
got, err := parseDateTime([]byte(cc.str), time.UTC)
if err != nil {
t.Fatal(err)
}

if !cc.want.Equal(got) {
t.Fatalf("want: %v, but got %v", cc.want, got)
}
})
}
}

func TestParseDateTimeFail(t *testing.T) {
cases := []struct {
name string
Expand Down