From 67b1102b5e9b345beb2ddcc529a8e608e5afc865 Mon Sep 17 00:00:00 2001 From: Zeke Lu Date: Thu, 17 Nov 2022 13:18:56 +0800 Subject: [PATCH] time: avoid creating a parse error from the next chunk of the value When it reports a parse error, it uses the "value" variable as the value element of the parse error. Previously, in some of the cases, the "value" variable is always updated to the next chunk of the value to be parsed (even if an earlier chunk is invalid). The reported parse error is confusing in this case. This CL addresses this issue by holding the original value, and when it fails to parse the time, use it to create the parse error. --- src/time/format.go | 12 +++++------- src/time/format_test.go | 20 ++++++++++---------- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/src/time/format.go b/src/time/format.go index d8d3709196f..e4f5750b0c9 100644 --- a/src/time/format.go +++ b/src/time/format.go @@ -1067,18 +1067,19 @@ func parse(layout, value string, defaultLocation, local *Location) (Time, error) } layout = suffix var p string + hold := value switch std & stdMask { case stdYear: if len(value) < 2 { err = errBad break } - hold := value p, value = value[0:2], value[2:] year, err = atoi(p) if err != nil { - value = hold - } else if year >= 69 { // Unix time starts Dec 31 1969 in some time zones + break + } + if year >= 69 { // Unix time starts Dec 31 1969 in some time zones year += 1900 } else { year += 2000 @@ -1295,10 +1296,7 @@ func parse(layout, value string, defaultLocation, local *Location) (Time, error) return Time{}, newParseError(alayout, avalue, stdstr, value, ": "+rangeErrString+" out of range") } if err != nil { - if p != "" { - return Time{}, newParseError(alayout, avalue, stdstr, p, "") - } - return Time{}, newParseError(alayout, avalue, stdstr, value, "") + return Time{}, newParseError(alayout, avalue, stdstr, hold, "") } } if pmSet && hour < 12 { diff --git a/src/time/format_test.go b/src/time/format_test.go index 7e4a92565d2..8a26eaa35ba 100644 --- a/src/time/format_test.go +++ b/src/time/format_test.go @@ -620,13 +620,13 @@ type ParseErrorTest struct { } var parseErrorTests = []ParseErrorTest{ - {ANSIC, "Feb 4 21:00:60 2010", "cannot parse"}, // cannot parse Feb as Mon - {ANSIC, "Thu Feb 4 21:00:57 @2010", "cannot parse"}, + {ANSIC, "Feb 4 21:00:60 2010", `cannot parse "Feb 4 21:00:60 2010" as "Mon"`}, + {ANSIC, "Thu Feb 4 21:00:57 @2010", `cannot parse "@2010" as "2006"`}, {ANSIC, "Thu Feb 4 21:00:60 2010", "second out of range"}, {ANSIC, "Thu Feb 4 21:61:57 2010", "minute out of range"}, {ANSIC, "Thu Feb 4 24:00:60 2010", "hour out of range"}, - {"Mon Jan _2 15:04:05.000 2006", "Thu Feb 4 23:00:59x01 2010", "cannot parse"}, - {"Mon Jan _2 15:04:05.000 2006", "Thu Feb 4 23:00:59.xxx 2010", "cannot parse"}, + {"Mon Jan _2 15:04:05.000 2006", "Thu Feb 4 23:00:59x01 2010", `cannot parse "x01 2010" as ".000"`}, + {"Mon Jan _2 15:04:05.000 2006", "Thu Feb 4 23:00:59.xxx 2010", `cannot parse ".xxx 2010" as ".000"`}, {"Mon Jan _2 15:04:05.000 2006", "Thu Feb 4 23:00:59.-123 2010", "fractional second out of range"}, // issue 4502. StampNano requires exactly 9 digits of precision. {StampNano, "Dec 7 11:22:01.000000", `cannot parse ".000000" as ".000000000"`}, @@ -641,8 +641,8 @@ var parseErrorTests = []ParseErrorTest{ // issue 54569 {RFC3339, "0000-01-01T00:00:.0+00:00", `parsing time "0000-01-01T00:00:.0+00:00" as "2006-01-02T15:04:05Z07:00": cannot parse ".0+00:00" as "05"`}, // issue 21113 - {"_2 Jan 06 15:04 MST", "4 --- 00 00:00 GMT", "cannot parse"}, - {"_2 January 06 15:04 MST", "4 --- 00 00:00 GMT", "cannot parse"}, + {"_2 Jan 06 15:04 MST", "4 --- 00 00:00 GMT", `cannot parse "--- 00 00:00 GMT" as "Jan"`}, + {"_2 January 06 15:04 MST", "4 --- 00 00:00 GMT", `cannot parse "--- 00 00:00 GMT" as "January"`}, // invalid or mismatched day-of-year {"Jan _2 002 2006", "Feb 4 034 2006", "day-of-year does not match day"}, @@ -653,12 +653,12 @@ var parseErrorTests = []ParseErrorTest{ {RFC3339, "\"", `parsing time "\"" as "2006-01-02T15:04:05Z07:00": cannot parse "\"" as "2006"`}, // issue 54570 - {RFC3339, "0000-01-01T00:00:00+00:+0", `parsing time "0000-01-01T00:00:00+00:+0" as "2006-01-02T15:04:05Z07:00": cannot parse "" as "Z07:00"`}, - {RFC3339, "0000-01-01T00:00:00+-0:00", `parsing time "0000-01-01T00:00:00+-0:00" as "2006-01-02T15:04:05Z07:00": cannot parse "" as "Z07:00"`}, + {RFC3339, "0000-01-01T00:00:00+00:+0", `parsing time "0000-01-01T00:00:00+00:+0" as "2006-01-02T15:04:05Z07:00": cannot parse "+00:+0" as "Z07:00"`}, + {RFC3339, "0000-01-01T00:00:00+-0:00", `parsing time "0000-01-01T00:00:00+-0:00" as "2006-01-02T15:04:05Z07:00": cannot parse "+-0:00" as "Z07:00"`}, // issue 56730 - {"2006-01-02", "22-10-25", `parsing time "22-10-25" as "2006-01-02": cannot parse "22-1" as "2006"`}, - {"06-01-02", "a2-10-25", `parsing time "a2-10-25" as "06-01-02": cannot parse "a2" as "06"`}, + {"2006-01-02", "22-10-25", `parsing time "22-10-25" as "2006-01-02": cannot parse "22-10-25" as "2006"`}, + {"06-01-02", "a2-10-25", `parsing time "a2-10-25" as "06-01-02": cannot parse "a2-10-25" as "06"`}, {"03:04PM", "12:03pM", `parsing time "12:03pM" as "03:04PM": cannot parse "pM" as "PM"`}, {"03:04pm", "12:03pM", `parsing time "12:03pM" as "03:04pm": cannot parse "pM" as "pm"`}, }