mirror of
https://github.com/araddon/dateparse.git
synced 2024-12-05 05:30:13 +08:00
work on #108, most likely wnf, this is incomplete
This commit is contained in:
parent
fcfe3a02eb
commit
89f84f141b
1
go.mod
1
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
|
||||
|
2
go.sum
2
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=
|
||||
|
28
parseany.go
28
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)
|
||||
}
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user