]> Cypherpunks repositories - gostls13.git/commitdiff
time: _2006 is a literal _, followed by 2006
authorEdward Muller <edwardam@interlix.com>
Sun, 25 Oct 2015 21:04:48 +0000 (14:04 -0700)
committerRob Pike <r@golang.org>
Wed, 18 Nov 2015 00:06:39 +0000 (00:06 +0000)
Otherwise _2006 is treated as _2 and then an error.

Fixes #11334

Change-Id: I40a385b45e279e9f4538bf419baab72781cdb215
Reviewed-on: https://go-review.googlesource.com/16311
Reviewed-by: Rob Pike <r@golang.org>
src/time/format.go
src/time/format_test.go

index d727ef09882d31dbf8289b3bfc052ca6e1141c92..697e1dd4ab74914401355eba17c5891104e9e140 100644 (file)
@@ -162,8 +162,12 @@ func nextStdChunk(layout string) (prefix string, std int, suffix string) {
                        }
                        return layout[0:i], stdDay, layout[i+1:]
 
-               case '_': // _2
+               case '_': // _2, _2006
                        if len(layout) >= i+2 && layout[i+1] == '2' {
+                               //_2006 is really a literal _, followed by stdLongYear
+                               if len(layout) >= i+5 && layout[i+1:i+5] == "2006" {
+                                       return layout[0 : i+1], stdLongYear, layout[i+5:]
+                               }
                                return layout[0:i], stdUnderDay, layout[i+2:]
                        }
 
index 8ff053d4d7c386b5e6cffb41f5f3813ac54d58f4..b1b7a005d41be8cef34321216eaba7e8e31b7c02 100644 (file)
@@ -529,3 +529,22 @@ func TestFormatSecondsInTimeZone(t *testing.T) {
                }
        }
 }
+
+// Issue 11334.
+func TestUnderscoreTwoThousand(t *testing.T) {
+       format := "15:04_20060102"
+       input := "14:38_20150618"
+       time, err := Parse(format, input)
+       if err != nil {
+               t.Error(err)
+       }
+       if y, m, d := time.Date(); y != 2015 || m != 6 || d != 18 {
+               t.Errorf("Incorrect y/m/d, got %d/%d/%d", y, m, d)
+       }
+       if h := time.Hour(); h != 14 {
+               t.Errorf("Incorrect hour, got %d", h)
+       }
+       if m := time.Minute(); m != 38 {
+               t.Errorf("Incorrect minute, got %d", m)
+       }
+}