aboutsummaryrefslogtreecommitdiff
path: root/src/time
diff options
context:
space:
mode:
authorIan Lance Taylor <iant@golang.org>2020-02-23 08:04:13 -0800
committerIan Lance Taylor <iant@golang.org>2020-02-23 18:06:08 +0000
commit5bd145413a84be1afa74a82767384d9e224f7069 (patch)
tree5b57c05452957db81297b1ca05ae9918d623125c /src/time
parentb0863ce0e6fbcf3e39f25cdd0b9380b3710507ba (diff)
downloadgo-5bd145413a84be1afa74a82767384d9e224f7069.tar.gz
go-5bd145413a84be1afa74a82767384d9e224f7069.zip
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>
Diffstat (limited to 'src/time')
-rw-r--r--src/time/format.go2
-rw-r--r--src/time/format_test.go14
2 files changed, 15 insertions, 1 deletions
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)
+ }
+ }
+}