]> Cypherpunks repositories - gostls13.git/commitdiff
time: fix Parse to ignore extra sub-nanosecond digits
authorJoe Tsai <joetsai@digital-static.net>
Sun, 21 Aug 2022 09:36:24 +0000 (02:36 -0700)
committerGopher Robot <gobot@golang.org>
Tue, 23 Aug 2022 17:19:32 +0000 (17:19 +0000)
This modifies the code to match the comment such that
the behavior truly is identical to stdSecond case.
Also, it modifies the behavior to match the documented
behavior where:

    Fractional seconds are truncated to nanosecond precision.

Fixes #54567
Updates #48685

Change-Id: Ie64549e4372ab51624c105ad8ab4cc99b9b5a0b3
Reviewed-on: https://go-review.googlesource.com/c/go/+/425037
Run-TryBot: Joseph Tsai <joetsai@digital-static.net>
Reviewed-by: David Chase <drchase@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Auto-Submit: Joseph Tsai <joetsai@digital-static.net>

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

index 80495c0266612ab109217913fffbb1a51648a37f..ed2a0a8fc683e45a12ea8de7fbc1393f74a57555 100644 (file)
@@ -1278,7 +1278,7 @@ func parse(layout, value string, defaultLocation, local *Location) (Time, error)
                        // Take any number of digits, even more than asked for,
                        // because it is what the stdSecond case would do.
                        i := 0
-                       for i < 9 && i+1 < len(value) && '0' <= value[i+1] && value[i+1] <= '9' {
+                       for i+1 < len(value) && '0' <= value[i+1] && value[i+1] <= '9' {
                                i++
                        }
                        nsec, rangeErrString, err = parseNanoseconds(value, 1+i)
index fa8c846686d8d7a4bc3573b76747363869b101c1..0e5b92440e4ca0fa0856f80822bd642cccff7e12 100644 (file)
@@ -863,7 +863,7 @@ func TestFormatFractionalSecondSeparators(t *testing.T) {
        }
 }
 
-// Issue 48685
+// Issue 48685 and 54567.
 func TestParseFractionalSecondsLongerThanNineDigits(t *testing.T) {
        tests := []struct {
                s    string
@@ -893,13 +893,15 @@ func TestParseFractionalSecondsLongerThanNineDigits(t *testing.T) {
        }
 
        for _, tt := range tests {
-               tm, err := Parse(RFC3339, tt.s)
-               if err != nil {
-                       t.Errorf("Unexpected error: %v", err)
-                       continue
-               }
-               if got := tm.Nanosecond(); got != tt.want {
-                       t.Errorf("Parse(%q) = got %d, want %d", tt.s, got, tt.want)
+               for _, format := range []string{RFC3339, RFC3339Nano} {
+                       tm, err := Parse(format, tt.s)
+                       if err != nil {
+                               t.Errorf("Parse(%q, %q) error: %v", format, tt.s, err)
+                               continue
+                       }
+                       if got := tm.Nanosecond(); got != tt.want {
+                               t.Errorf("Parse(%q, %q) = got %d, want %d", format, tt.s, got, tt.want)
+                       }
                }
        }
 }