1
0
mirror of https://github.com/golang/go synced 2024-11-17 19:54:45 -07:00

time: don't get confused about day 31 when parsing 002

The 002 parsing code had a bug that mishandled day 31.

Fixes #37387

Change-Id: Ia5a492a4ddd09a4bc232ce9582aead42d5099bdd
Reviewed-on: https://go-review.googlesource.com/c/go/+/220637
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Tobias Klauser <tobias.klauser@gmail.com>
This commit is contained in:
Ian Lance Taylor 2020-02-23 08:04:13 -08:00
parent b0863ce0e6
commit 5bd145413a
2 changed files with 15 additions and 1 deletions

View File

@ -1112,7 +1112,7 @@ func parse(layout, value string, defaultLocation, local *Location) (Time, error)
return Time{}, &ParseError{alayout, avalue, "", value, ": day-of-year out of range"} return Time{}, &ParseError{alayout, avalue, "", value, ": day-of-year out of range"}
} }
if m == 0 { if m == 0 {
m = yday/31 + 1 m = (yday-1)/31 + 1
if int(daysBefore[m]) < yday { if int(daysBefore[m]) < yday {
m++ m++
} }

View File

@ -756,3 +756,17 @@ func TestParseMonthOutOfRange(t *testing.T) {
} }
} }
} }
// Issue 37387.
func TestParseYday(t *testing.T) {
t.Parallel()
for i := 1; i <= 365; i++ {
d := fmt.Sprintf("2020-%03d", i)
tm, err := Parse("2006-002", d)
if err != nil {
t.Errorf("unexpected error for %s: %v", d, err)
} else if tm.Year() != 2020 || tm.YearDay() != i {
t.Errorf("got year %d yearday %d, want %d %d", tm.Year(), tm.YearDay(), 2020, i)
}
}
}