diff --git a/go.mod b/go.mod index 8927e11..e13566d 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.12 require ( github.com/apcera/termtables v0.0.0-20170405184538-bcbc5dc54055 // indirect + github.com/araddon/gou v0.0.0-20190110011759-c797efecbb61 github.com/mattn/go-runewidth v0.0.9 // indirect github.com/scylladb/termtables v1.0.0 github.com/stretchr/testify v1.6.1 diff --git a/go.sum b/go.sum index d412c85..0912f8b 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ github.com/apcera/termtables v0.0.0-20170405184538-bcbc5dc54055 h1:IkPAzP+QjchKXXFX6LCcpDKa89b/e/0gPCUbQGWtUUY= github.com/apcera/termtables v0.0.0-20170405184538-bcbc5dc54055/go.mod h1:8mHYHlOef9UC51cK1/WRvE/iQVM8O8QlYFa8eh8r5I8= +github.com/araddon/gou v0.0.0-20190110011759-c797efecbb61 h1:Xz25cuW4REGC5W5UtpMU3QItMIImag615HiQcRbxqKQ= +github.com/araddon/gou v0.0.0-20190110011759-c797efecbb61/go.mod h1:ikc1XA58M+Rx7SEbf0bLJCfBkwayZ8T5jBo5FXK8Uz8= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0= diff --git a/parseany.go b/parseany.go index 61a021e..e7ae7d8 100644 --- a/parseany.go +++ b/parseany.go @@ -10,12 +10,14 @@ import ( "time" "unicode" "unicode/utf8" + + "github.com/araddon/gou" ) -// func init() { -// gou.SetupLogging("debug") -// gou.SetColorOutput() -// } +func init() { + gou.SetupLogging("debug") + gou.SetColorOutput() +} var days = []string{ "mon", @@ -204,14 +206,27 @@ func MustParse(datestr string, opts ...ParserOption) time.Time { // // layout = "2006-01-02 15:04:05" // func ParseFormat(datestr string, opts ...ParserOption) (string, error) { - p, err := parseTime(datestr, nil, opts...) + return parseFormatOptions(datestr, nil, opts...) +} +func parseFormatOptions(datestr string, loc *time.Location, opts ...ParserOption) (string, error) { + p, err := parseTime(datestr, loc, opts...) if err != nil { return "", err } - _, err = p.parse() + t, err := p.parse() + gou.Debugf("%s returned %v IsZero()=%v nano=%v second=%v error %v", datestr, t, t.IsZero(), t.UnixNano(), t.Unix(), err) if err != nil { return "", err } + + // Because the above parse essentially punts by sending to underlying + // go time.Parse() which can easily return a 0000-01-01 00:00:00 +0000 UTC + // we count that as an error. + // + // However, this is definitely not failproof. + if t.IsZero() { + return "", fmt.Errorf("%s returned a IsZero() time so must be invalid", datestr) + } return string(p.format), nil } @@ -2002,7 +2017,6 @@ func (p *parser) parse() (time.Time, error) { p.format = p.format[p.skip:] p.datestr = p.datestr[p.skip:] } - //gou.Debugf("parse %q AS %q", p.datestr, string(p.format)) if p.loc == nil { return time.Parse(string(p.format), p.datestr) } diff --git a/parseany_test.go b/parseany_test.go index 35e212c..d2dcec4 100644 --- a/parseany_test.go +++ b/parseany_test.go @@ -527,11 +527,19 @@ var testParseFormat = []dateTest{ {in: "2009-08-12T22:15:09-0700", out: "2006-01-02T15:04:05-0700"}, // yyyy-mm-ddThh:mm:ssZ {in: "2009-08-12T22:15Z", out: "2006-01-02T15:04Z"}, + // others + {in: "31.jpg", err: true}, + //{in: "1.jpg", err: true}, This SHOULD error but doesn't, our validating tests are really + // just passing down to underlying go error which this doesn't catch. } -func TestParseLayout(t *testing.T) { +func TestParseFormat(t *testing.T) { + _, err := parseFormatOptions("3.jpg", time.UTC) + assert.NotEqual(t, nil, err) + _, err = ParseFormat(`{"hello"}`) + assert.NotEqual(t, nil, err) for _, th := range testParseFormat { - l, err := ParseFormat(th.in) + l, err := parseFormatOptions(th.in, time.UTC) if th.err { assert.NotEqual(t, nil, err) } else {