Skip to content

Commit fa1e4ed

Browse files
authored
Fix parsing 0 year. (#1257)
Fix #1252
1 parent 05bed83 commit fa1e4ed

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

utils.go

-10
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,6 @@ func parseDateTime(b []byte, loc *time.Location) (time.Time, error) {
117117
if err != nil {
118118
return time.Time{}, err
119119
}
120-
if year <= 0 {
121-
year = 1
122-
}
123-
124120
if b[4] != '-' {
125121
return time.Time{}, fmt.Errorf("bad value for field: `%c`", b[4])
126122
}
@@ -129,9 +125,6 @@ func parseDateTime(b []byte, loc *time.Location) (time.Time, error) {
129125
if err != nil {
130126
return time.Time{}, err
131127
}
132-
if m <= 0 {
133-
m = 1
134-
}
135128
month := time.Month(m)
136129

137130
if b[7] != '-' {
@@ -142,9 +135,6 @@ func parseDateTime(b []byte, loc *time.Location) (time.Time, error) {
142135
if err != nil {
143136
return time.Time{}, err
144137
}
145-
if day <= 0 {
146-
day = 1
147-
}
148138
if len(b) == 10 {
149139
return time.Date(year, month, day, 0, 0, 0, 0, loc), nil
150140
}

utils_test.go

+27
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,33 @@ func TestParseDateTime(t *testing.T) {
380380
}
381381
}
382382

383+
func TestInvalidDateTime(t *testing.T) {
384+
cases := []struct {
385+
name string
386+
str string
387+
want time.Time
388+
}{
389+
{
390+
name: "parse datetime without day",
391+
str: "0000-00-00 21:30:45",
392+
want: time.Date(0, 0, 0, 21, 30, 45, 0, time.UTC),
393+
},
394+
}
395+
396+
for _, cc := range cases {
397+
t.Run(cc.name, func(t *testing.T) {
398+
got, err := parseDateTime([]byte(cc.str), time.UTC)
399+
if err != nil {
400+
t.Fatal(err)
401+
}
402+
403+
if !cc.want.Equal(got) {
404+
t.Fatalf("want: %v, but got %v", cc.want, got)
405+
}
406+
})
407+
}
408+
}
409+
383410
func TestParseDateTimeFail(t *testing.T) {
384411
cases := []struct {
385412
name string

0 commit comments

Comments
 (0)