From: Rob Pike Date: Mon, 31 Aug 2015 22:45:47 +0000 (-0700) Subject: time: allow any one- or two-digit day of the month when parsing. X-Git-Tag: go1.6beta1~1114 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=8b96be15f6632b33c7341840002bc5b0744b2977;p=gostls13.git time: allow any one- or two-digit day of the month when parsing. In Parse, one can now say Feb 31 or even Feb 99. This is easy to explain, consistent with time.Date, and even maybe useful. Fixes #12333. Fixes #7268. (By disagreeing with it.) Change-Id: I7b95c842528bed66933681c8b9cc00640fccfcb4 Reviewed-on: https://go-review.googlesource.com/14123 Reviewed-by: Ian Lance Taylor --- diff --git a/src/time/format.go b/src/time/format.go index 6cf7946711..d727ef0988 100644 --- a/src/time/format.go +++ b/src/time/format.go @@ -696,6 +696,11 @@ func skip(value, prefix string) (string, error) { // location and zone in the returned time. Otherwise it records the time as // being in a fabricated location with time fixed at the given zone offset. // +// No checking is done that the day of the month is within the month's +// valid dates; any one- or two-digit value is accepted. For example +// February 31 and even February 99 are valid dates, specifying dates +// in March and May. This behavior is consistent with time.Date. +// // When parsing a time with a zone abbreviation like MST, if the zone abbreviation // has a defined offset in the current location, then that offset is used. // The zone abbreviation "UTC" is recognized as UTC regardless of location. @@ -794,7 +799,8 @@ func parse(layout, value string, defaultLocation, local *Location) (Time, error) value = value[1:] } day, value, err = getnum(value, std == stdZeroDay) - if day < 0 || 31 < day { + if day < 0 { + // Note that we allow any one- or two-digit day here. rangeErrString = "day" } case stdHour: diff --git a/src/time/format_test.go b/src/time/format_test.go index d44347aed5..8ff053d4d7 100644 --- a/src/time/format_test.go +++ b/src/time/format_test.go @@ -164,6 +164,9 @@ var parseTests = []ParseTest{ // GMT with offset. {"GMT-8", UnixDate, "Fri Feb 5 05:00:57 GMT-8 2010", true, true, 1, 0}, + // Day of month can be out of range. + {"Jan 36", UnixDate, "Fri Jan 36 05:00:57 GMT-8 2010", true, true, 1, 0}, + // Accept any number of fractional second digits (including none) for .999... // In Go 1, .999... was completely ignored in the format, meaning the first two // cases would succeed, but the next four would not. Go 1.1 accepts all six.