Commit 8b96be15 authored by Rob Pike's avatar Rob Pike

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/14123Reviewed-by: 's avatarIan Lance Taylor <iant@golang.org>
parent e216735d
...@@ -696,6 +696,11 @@ func skip(value, prefix string) (string, error) { ...@@ -696,6 +696,11 @@ func skip(value, prefix string) (string, error) {
// location and zone in the returned time. Otherwise it records the time as // 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. // 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 // 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. // has a defined offset in the current location, then that offset is used.
// The zone abbreviation "UTC" is recognized as UTC regardless of location. // 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) ...@@ -794,7 +799,8 @@ func parse(layout, value string, defaultLocation, local *Location) (Time, error)
value = value[1:] value = value[1:]
} }
day, value, err = getnum(value, std == stdZeroDay) 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" rangeErrString = "day"
} }
case stdHour: case stdHour:
......
...@@ -164,6 +164,9 @@ var parseTests = []ParseTest{ ...@@ -164,6 +164,9 @@ var parseTests = []ParseTest{
// GMT with offset. // GMT with offset.
{"GMT-8", UnixDate, "Fri Feb 5 05:00:57 GMT-8 2010", true, true, 1, 0}, {"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... // 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 // 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. // cases would succeed, but the next four would not. Go 1.1 accepts all six.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment