fix "3" too short for date, force error closes #56

This commit is contained in:
Aaron Raddon 2018-04-09 18:54:05 -07:00
parent 647d17def2
commit b8014f0dba
2 changed files with 14 additions and 10 deletions

View File

@ -1295,6 +1295,8 @@ iterRunes:
} else if len(datestr) == len("2014") { } else if len(datestr) == len("2014") {
p.format = []byte("2006") p.format = []byte("2006")
return p, nil return p, nil
} else if len(datestr) < 4 {
return nil, fmt.Errorf("unrecognized format, to short %v", datestr)
} }
if t.IsZero() { if t.IsZero() {
if secs, err := strconv.ParseInt(datestr, 10, 64); err == nil { if secs, err := strconv.ParseInt(datestr, 10, 64); err == nil {

View File

@ -132,6 +132,7 @@ func TestOne(t *testing.T) {
type dateTest struct { type dateTest struct {
in, out, loc string in, out, loc string
err bool
} }
// {in: , out: }, // {in: , out: },
@ -518,6 +519,11 @@ func TestPStruct(t *testing.T) {
} }
var testParseFormat = []dateTest{ var testParseFormat = []dateTest{
// errors
{in: "3", err: true},
{in: `{"hello"}`, err: true},
{in: "2009-15-12T22:15Z", err: true},
//
{in: "oct 7, 1970", out: "Jan 2, 2006"}, {in: "oct 7, 1970", out: "Jan 2, 2006"},
// 03 February 2013 // 03 February 2013
{in: "03 February 2013", out: "02 January 2006"}, {in: "03 February 2013", out: "02 January 2006"},
@ -538,19 +544,15 @@ var testParseFormat = []dateTest{
} }
func TestParseLayout(t *testing.T) { func TestParseLayout(t *testing.T) {
for _, th := range testParseFormat { for _, th := range testParseFormat {
l, err := ParseFormat(th.in) l, err := ParseFormat(th.in)
assert.Equal(t, nil, err) if th.err {
assert.Equal(t, th.out, l, "for in=%v", th.in) assert.NotEqual(t, nil, err)
} else {
assert.Equal(t, nil, err)
assert.Equal(t, th.out, l, "for in=%v", th.in)
}
} }
// errors
_, err := ParseFormat(`{"hello"}`)
assert.NotEqual(t, nil, err)
_, err = ParseFormat("2009-15-12T22:15Z")
assert.NotEqual(t, nil, err)
} }
var testParseStrict = []dateTest{ var testParseStrict = []dateTest{