From 5bd145413a84be1afa74a82767384d9e224f7069 Mon Sep 17 00:00:00 2001 From: Ian Lance Taylor Date: Sun, 23 Feb 2020 08:04:13 -0800 Subject: [PATCH] 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 TryBot-Result: Gobot Gobot Reviewed-by: Tobias Klauser --- src/time/format.go | 2 +- src/time/format_test.go | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/time/format.go b/src/time/format.go index 9beb5d9a48..899b6a40b0 100644 --- a/src/time/format.go +++ b/src/time/format.go @@ -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"} } if m == 0 { - m = yday/31 + 1 + m = (yday-1)/31 + 1 if int(daysBefore[m]) < yday { m++ } diff --git a/src/time/format_test.go b/src/time/format_test.go index 34990cdbc3..a030242e6a 100644 --- a/src/time/format_test.go +++ b/src/time/format_test.go @@ -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) + } + } +}