mirror of
https://github.com/araddon/dateparse.git
synced 2025-01-18 18:56:47 +08:00
More date formats closes #46
This commit is contained in:
parent
62e766bf9e
commit
a653ec7165
45
parseany.go
45
parseany.go
@ -278,6 +278,7 @@ func (p *parser) parse() (time.Time, error) {
|
||||
p.format = p.format[p.skip:]
|
||||
p.datestr = p.datestr[p.skip:]
|
||||
}
|
||||
//gou.Debugf("parse %q AS %s", p.datestr, string(p.format))
|
||||
if p.loc == nil {
|
||||
return time.Parse(string(p.format), p.datestr)
|
||||
}
|
||||
@ -350,6 +351,7 @@ iterRunes:
|
||||
case ' ':
|
||||
// 18 January 2018
|
||||
// 8 January 2018
|
||||
// 8 jan 2018
|
||||
// 02 Jan 2018 23:59
|
||||
// 02 Jan 2018 23:59:34
|
||||
// 12 Feb 2006, 19:17
|
||||
@ -486,6 +488,8 @@ iterRunes:
|
||||
case dateDigitWs:
|
||||
// 18 January 2018
|
||||
// 8 January 2018
|
||||
// 8 jan 2018
|
||||
// 1 jan 18
|
||||
// 02 Jan 2018 23:59
|
||||
// 02 Jan 2018 23:59:34
|
||||
// 12 Feb 2006, 19:17
|
||||
@ -493,7 +497,7 @@ iterRunes:
|
||||
switch r {
|
||||
case ' ':
|
||||
p.yeari = i + 1
|
||||
p.yearlen = 4
|
||||
//p.yearlen = 4
|
||||
p.dayi = 0
|
||||
p.daylen = p.part1Len
|
||||
p.setDay()
|
||||
@ -509,6 +513,7 @@ iterRunes:
|
||||
}
|
||||
|
||||
case dateDigitWsMoYear:
|
||||
// 8 jan 2018
|
||||
// 02 Jan 2018 23:59
|
||||
// 02 Jan 2018 23:59:34
|
||||
// 12 Feb 2006, 19:17
|
||||
@ -552,6 +557,7 @@ iterRunes:
|
||||
// Fri Jul 03 2015 18:04:07 GMT+0100 (GMT Daylight Time)
|
||||
// dateAlphaWSDigit
|
||||
// May 8, 2009 5:57:51 PM
|
||||
// oct 1, 1970
|
||||
//
|
||||
// dateWeekdayComma
|
||||
// Monday, 02 Jan 2006 15:04:05 MST
|
||||
@ -652,6 +658,8 @@ iterRunes:
|
||||
// Mon Aug 10 15:44:11 UTC+0100 2015
|
||||
// dateAlphaWsDigit
|
||||
// May 8, 2009 5:57:51 PM
|
||||
// oct 1, 1970
|
||||
// oct 7, '70
|
||||
switch {
|
||||
case unicode.IsLetter(r):
|
||||
p.set(0, "Mon")
|
||||
@ -665,24 +673,33 @@ iterRunes:
|
||||
}
|
||||
|
||||
case dateAlphaWsDigit:
|
||||
// dateAlphaWsDigit
|
||||
// May 8, 2009 5:57:51 PM
|
||||
// May 8, 2009 5:57:51 PM
|
||||
// oct 1, 1970
|
||||
// oct 7, '70
|
||||
//gou.Debugf("%d %s dateAlphaWsDigit %s %s", i, string(r), p.ds(), p.ts())
|
||||
if r == ',' {
|
||||
p.daylen = i - p.dayi
|
||||
p.setDay()
|
||||
p.stateDate = dateAlphaWsDigitComma
|
||||
}
|
||||
case dateAlphaWsDigitComma:
|
||||
// x
|
||||
// May 8, 2009 5:57:51 PM
|
||||
// x
|
||||
// May 8, 2009 5:57:51 PM
|
||||
// oct 1, 1970
|
||||
// oct 7, '70
|
||||
if r == ' ' {
|
||||
p.stateDate = dateAlphaWsDigitCommaWs
|
||||
p.yeari = i + 1
|
||||
}
|
||||
case dateAlphaWsDigitCommaWs:
|
||||
// x
|
||||
// May 8, 2009 5:57:51 PM
|
||||
if r == ' ' {
|
||||
// x
|
||||
// May 8, 2009 5:57:51 PM
|
||||
// oct 1, 1970
|
||||
// oct 7, '70
|
||||
switch r {
|
||||
case '\'':
|
||||
p.yeari = i + 1
|
||||
case ' ':
|
||||
p.stateDate = dateAlphaWsDigitCommaWsYear
|
||||
p.yearlen = i - p.yeari
|
||||
p.setYear()
|
||||
@ -732,7 +749,9 @@ iterRunes:
|
||||
p.coalesceDate(i)
|
||||
if p.stateTime == timeStart {
|
||||
// increment first one, since the i++ occurs at end of loop
|
||||
i++
|
||||
if i < len(p.datestr) {
|
||||
i++
|
||||
}
|
||||
|
||||
iterTimeRunes:
|
||||
for ; i < len(datestr); i++ {
|
||||
@ -1192,6 +1211,8 @@ iterRunes:
|
||||
return p.parse()
|
||||
|
||||
case dateDigitWsMoYear:
|
||||
// 2 Jan 2018
|
||||
// 2 Jan 18
|
||||
// 2 Jan 2018 23:59
|
||||
// 02 Jan 2018 23:59
|
||||
// 02 Jan 2018 23:59:45
|
||||
@ -1207,6 +1228,12 @@ iterRunes:
|
||||
}
|
||||
return parse("2 January 2006", datestr, loc)
|
||||
|
||||
case dateAlphaWsDigitCommaWs:
|
||||
// oct 1, 1970
|
||||
p.yearlen = i - p.yeari
|
||||
p.setYear()
|
||||
return p.parse()
|
||||
|
||||
case dateAlphaWsDigitCommaWsYear:
|
||||
// May 8, 2009 5:57:51 PM
|
||||
return p.parse()
|
||||
|
@ -119,6 +119,18 @@ func TestOne(t *testing.T) {
|
||||
ts = MustParse("2014-05-11 08:20:13,787")
|
||||
assert.Equal(t, "2014-05-11 08:20:13.787 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))
|
||||
}
|
||||
|
||||
type dateTest struct {
|
||||
in, out string
|
||||
}
|
||||
|
||||
var testInputs = []dateTest{
|
||||
{in: "oct 7, 1970", out: "1970-10-07 00:00:00 +0000 UTC"},
|
||||
{in: "oct 7, '70", out: "1970-10-07 00:00:00 +0000 UTC"},
|
||||
{in: "7 oct 70", out: "1970-10-07 00:00:00 +0000 UTC"},
|
||||
{in: "7 oct 1970", out: "1970-10-07 00:00:00 +0000 UTC"},
|
||||
}
|
||||
|
||||
func TestParse(t *testing.T) {
|
||||
|
||||
/*
|
||||
@ -138,6 +150,12 @@ func TestParse(t *testing.T) {
|
||||
|
||||
assert.Equal(t, true, testDidPanic("NOT GONNA HAPPEN"))
|
||||
|
||||
for _, th := range testInputs {
|
||||
ts = MustParse(th.in)
|
||||
got := fmt.Sprintf("%v", ts.In(time.UTC))
|
||||
assert.Equal(t, th.out, got, "Expected %q but got %q from %q", th.out, got, th.in)
|
||||
}
|
||||
|
||||
// TODO: Is a utf8 date valid?
|
||||
// ts = MustParse("2014-04\u221226")
|
||||
// assert.Equal(t, "2014-04-26 00:00:00 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))
|
||||
|
Loading…
Reference in New Issue
Block a user