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