]> Cypherpunks repositories - gostls13.git/commitdiff
time: avoid creating a parse error from the next chunk of the value
authorZeke Lu <lvzecai@gmail.com>
Thu, 17 Nov 2022 12:19:49 +0000 (12:19 +0000)
committerGopher Robot <gobot@golang.org>
Thu, 17 Nov 2022 15:49:25 +0000 (15:49 +0000)
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.

Fixes #56730.

Change-Id: I445b1d8a1b910208d0608b2186881746adb550e0
GitHub-Last-Rev: 67b1102b5e9b345beb2ddcc529a8e608e5afc865
GitHub-Pull-Request: golang/go#56754
Reviewed-on: https://go-review.googlesource.com/c/go/+/450936
Reviewed-by: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Joedian Reid <joedian@golang.org>
Auto-Submit: Bryan Mills <bcmills@google.com>
Run-TryBot: Bryan Mills <bcmills@google.com>

src/time/format.go
src/time/format_test.go

index 89a3ce259cf72bd40c9884e6ab72087f0af2c23f..e4f5750b0c92327c613d58e3ac4ddc55b348fcfe 100644 (file)
@@ -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,7 +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 {
-                       return Time{}, newParseError(alayout, avalue, stdstr, value, "")
+                       return Time{}, newParseError(alayout, avalue, stdstr, hold, "")
                }
        }
        if pmSet && hour < 12 {
index b1d85f510be4c9f5875eeddb6937ec8c102ca4c0..8a26eaa35bab10c25f5faf49ed72e22f61e256a4 100644 (file)
@@ -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,8 +653,14 @@ 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-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"`},
 }
 
 func TestParseErrors(t *testing.T) {