mirror of
https://github.com/araddon/dateparse.git
synced 2025-01-19 11:16:12 +08:00
Skip white space
This commit is contained in:
parent
19ef6a25eb
commit
8b765a5302
@ -1097,10 +1097,12 @@ iterRunes:
|
|||||||
// Thu, 4 Jan 2018 17:53:36 +0000
|
// Thu, 4 Jan 2018 17:53:36 +0000
|
||||||
// Tue, 11 Jul 2017 16:28:13 +0200 (CEST)
|
// Tue, 11 Jul 2017 16:28:13 +0200 (CEST)
|
||||||
// Mon, 02-Jan-06 15:04:05 MST
|
// Mon, 02-Jan-06 15:04:05 MST
|
||||||
|
var offset int
|
||||||
switch r {
|
switch r {
|
||||||
case ' ':
|
case ' ':
|
||||||
for i+1 < len(datestr) && datestr[i+1] == ' ' {
|
for i+1 < len(datestr) && datestr[i+1] == ' ' {
|
||||||
i++
|
i++
|
||||||
|
offset++
|
||||||
}
|
}
|
||||||
fallthrough
|
fallthrough
|
||||||
case '-':
|
case '-':
|
||||||
@ -1111,11 +1113,11 @@ iterRunes:
|
|||||||
p.setDay()
|
p.setDay()
|
||||||
p.moi = i + 1
|
p.moi = i + 1
|
||||||
} else if p.yeari == 0 {
|
} else if p.yeari == 0 {
|
||||||
p.molen = i - p.moi
|
p.molen = i - p.moi - offset
|
||||||
p.set(p.moi, "Jan")
|
p.set(p.moi, "Jan")
|
||||||
p.yeari = i + 1
|
p.yeari = i + 1
|
||||||
} else {
|
} else {
|
||||||
p.yearlen = i - p.yeari
|
p.yearlen = i - p.yeari - offset
|
||||||
p.setYear()
|
p.setYear()
|
||||||
p.stateTime = timeStart
|
p.stateTime = timeStart
|
||||||
break iterRunes
|
break iterRunes
|
||||||
|
@ -427,6 +427,7 @@ func TestParse(t *testing.T) {
|
|||||||
time.Local = time.UTC
|
time.Local = time.UTC
|
||||||
|
|
||||||
zeroTime := time.Time{}.Unix()
|
zeroTime := time.Time{}.Unix()
|
||||||
|
t.Run("Invalid", func(t *testing.T) {
|
||||||
ts, err := ParseAny("INVALID")
|
ts, err := ParseAny("INVALID")
|
||||||
assert.Equal(t, zeroTime, ts.Unix())
|
assert.Equal(t, zeroTime, ts.Unix())
|
||||||
assert.NotEqual(t, nil, err)
|
assert.NotEqual(t, nil, err)
|
||||||
@ -435,9 +436,11 @@ func TestParse(t *testing.T) {
|
|||||||
// https://github.com/golang/go/issues/5294
|
// https://github.com/golang/go/issues/5294
|
||||||
_, err = ParseAny(time.RFC3339)
|
_, err = ParseAny(time.RFC3339)
|
||||||
assert.NotEqual(t, nil, err)
|
assert.NotEqual(t, nil, err)
|
||||||
|
})
|
||||||
|
|
||||||
for _, th := range testInputs {
|
for _, th := range testInputs {
|
||||||
t.Run(th.in, func(t *testing.T) {
|
t.Run(th.in, func(t *testing.T) {
|
||||||
|
var ts time.Time
|
||||||
defer func() {
|
defer func() {
|
||||||
if r := recover(); r != nil {
|
if r := recover(); r != nil {
|
||||||
t.Fatalf("error: %s", r)
|
t.Fatalf("error: %s", r)
|
||||||
@ -470,16 +473,24 @@ func TestParse(t *testing.T) {
|
|||||||
|
|
||||||
// some errors
|
// some errors
|
||||||
|
|
||||||
|
t.Run("", func(t *testing.T) {
|
||||||
assert.Equal(t, true, testDidPanic(`{"ts":"now"}`))
|
assert.Equal(t, true, testDidPanic(`{"ts":"now"}`))
|
||||||
|
})
|
||||||
|
|
||||||
_, err = ParseAny("138421636711122233311111") // too many digits
|
t.Run("too many digits", func(t *testing.T) {
|
||||||
|
_, err := ParseAny("138421636711122233311111") // too many digits
|
||||||
assert.NotEqual(t, nil, err)
|
assert.NotEqual(t, nil, err)
|
||||||
|
})
|
||||||
|
|
||||||
_, err = ParseAny("-1314")
|
t.Run("negative number", func(t *testing.T) {
|
||||||
|
_, err := ParseAny("-1314")
|
||||||
assert.NotEqual(t, nil, err)
|
assert.NotEqual(t, nil, err)
|
||||||
|
})
|
||||||
|
|
||||||
_, err = ParseAny("2014-13-13 08:20:13,787") // month 13 doesn't exist so error
|
t.Run("month doesn't exist", func(t *testing.T) {
|
||||||
|
_, err := ParseAny("2014-13-13 08:20:13,787") // month 13 doesn't exist so error
|
||||||
assert.NotEqual(t, nil, err)
|
assert.NotEqual(t, nil, err)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func testDidPanic(datestr string) (paniced bool) {
|
func testDidPanic(datestr string) (paniced bool) {
|
||||||
|
Loading…
Reference in New Issue
Block a user