1
0
mirror of https://github.com/golang/go synced 2024-09-30 04:24:29 -06:00

time: _2006 is a literal _, followed by 2006

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>
This commit is contained in:
Edward Muller 2015-10-25 14:04:48 -07:00 committed by Rob Pike
parent 2619dccf3c
commit f4b4d2f4d9
2 changed files with 24 additions and 1 deletions

View 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:]
}

View 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)
}
}