Skip to content

Commit

Permalink
Fix parsing 0 year. (#1257)
Browse files Browse the repository at this point in the history
Fix #1252
  • Loading branch information
methane committed Nov 10, 2022
1 parent 05bed83 commit fa1e4ed
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
10 changes: 0 additions & 10 deletions utils.go
Expand Up @@ -117,10 +117,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 @@ -129,9 +125,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 @@ -142,9 +135,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 @@ -380,6 +380,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

0 comments on commit fa1e4ed

Please sign in to comment.