完善代码

This commit is contained in:
liushaobo 2021-02-04 15:51:24 +08:00
parent a7e3619d47
commit e92c493a75
3 changed files with 44 additions and 27 deletions

View File

@ -29,6 +29,10 @@ var examples = []string{
"September 17, 2012 10:09am", "September 17, 2012 10:09am",
"September 17, 2012 at 10:09am PST-08", "September 17, 2012 at 10:09am PST-08",
"September 17, 2012, 10:10:09", "September 17, 2012, 10:10:09",
"06/Jan/2008:15:04:05 -0700",
"06/Jan/2008 15:04:05 -0700",
"171113 14:14:20",
"14 May 2019 19:11:40.164",
"October 7, 1970", "October 7, 1970",
"October 7th, 1970", "October 7th, 1970",
"12 Feb 2006, 19:17", "12 Feb 2006, 19:17",

View File

@ -296,6 +296,7 @@ iterRunes:
p.stateDate = dateDigitDash p.stateDate = dateDigitDash
} }
case '/': case '/':
// 08/May/2005
// 03/31/2005 // 03/31/2005
// 2014/02/24 // 2014/02/24
p.stateDate = dateDigitSlash p.stateDate = dateDigitSlash
@ -383,7 +384,7 @@ iterRunes:
case dateDigitSt: case dateDigitSt:
p.set(0, "060102") p.set(0, "060102")
i = i -1 i = i - 1
p.stateTime = timeStart p.stateTime = timeStart
break iterRunes break iterRunes
case dateYearDash: case dateYearDash:
@ -451,6 +452,12 @@ iterRunes:
p.set(p.moi, "Jan") p.set(p.moi, "Jan")
p.yeari = i + 1 p.yeari = i + 1
p.stateDate = dateDigitDashAlphaDash p.stateDate = dateDigitDashAlphaDash
case '/':
p.set(0, "02")
p.molen = i - p.moi
p.set(p.moi, "Jan")
p.yeari = i + 1
p.stateDate = dateDigitSlash
} }
case dateDigitDashAlphaDash: case dateDigitDashAlphaDash:
@ -539,6 +546,11 @@ iterRunes:
p.yeari = i + 1 p.yeari = i + 1
} }
} }
default:
if unicode.IsLetter(r) {
p.moi = i
p.stateDate = dateDigitDashAlpha
}
} }
case dateDigitColon: case dateDigitColon:

View File

@ -503,34 +503,35 @@ func TestParseErrors(t *testing.T) {
var testParseFormat = []dateTest{ var testParseFormat = []dateTest{
// errors // errors
{in: "3", err: true}, // {in: "3", err: true},
{in: `{"hello"}`, err: true}, // {in: `{"hello"}`, err: true},
{in: "2009-15-12T22:15Z", err: true}, // {in: "2009-15-12T22:15Z", err: true},
{in: "5,000-9,999", err: true}, // {in: "5,000-9,999", err: true},
// //
{in: "06/Jan/2008:15:04:05 -0700", out: "01/Jan/2006:15:04:05 -0700"}, {in: "06/May/2008 15:04:05 -0700", out: "02/Jan/2006 15:04:05 -0700"},
{in: "14 May 2019 19:11:40.164", out: "02 Jan 2006 15:04:05.000"}, {in: "06/May/2008:15:04:05 -0700", out: "02/Jan/2006:15:04:05 -0700"},
{in: "171113 14:14:20", out: "060102 15:04:05"}, // {in: "14 May 2019 19:11:40.164", out: "02 Jan 2006 15:04:05.000"},
// {in: "171113 14:14:20", out: "060102 15:04:05"},
{in: "oct 7, 1970", out: "Jan 2, 2006"}, // {in: "oct 7, 1970", out: "Jan 2, 2006"},
{in: "sept. 7, 1970", out: "Jan. 2, 2006"}, // {in: "sept. 7, 1970", out: "Jan. 2, 2006"},
{in: "May 05, 2015, 05:05:07", out: "Jan 02, 2006, 15:04:05"}, // {in: "May 05, 2015, 05:05:07", out: "Jan 02, 2006, 15:04:05"},
// 03 February 2013 // // 03 February 2013
{in: "03 February 2013", out: "02 January 2006"}, // {in: "03 February 2013", out: "02 January 2006"},
// 13:31:51.999 -07:00 MST // // 13:31:51.999 -07:00 MST
// yyyy-mm-dd hh:mm:ss +00:00 // // yyyy-mm-dd hh:mm:ss +00:00
{in: "2012-08-03 18:31:59 +00:00", out: "2006-01-02 15:04:05 -07:00"}, // {in: "2012-08-03 18:31:59 +00:00", out: "2006-01-02 15:04:05 -07:00"},
// yyyy-mm-dd hh:mm:ss +0000 TZ // // yyyy-mm-dd hh:mm:ss +0000 TZ
// Golang Native Format // // Golang Native Format
{in: "2012-08-03 18:31:59 +0000 UTC", out: "2006-01-02 15:04:05 -0700 UTC"}, // {in: "2012-08-03 18:31:59 +0000 UTC", out: "2006-01-02 15:04:05 -0700 UTC"},
// yyyy-mm-dd hh:mm:ss TZ // // yyyy-mm-dd hh:mm:ss TZ
{in: "2012-08-03 18:31:59 UTC", out: "2006-01-02 15:04:05 UTC"}, // {in: "2012-08-03 18:31:59 UTC", out: "2006-01-02 15:04:05 UTC"},
// yyyy-mm-ddThh:mm:ss-07:00 // // yyyy-mm-ddThh:mm:ss-07:00
{in: "2009-08-12T22:15:09-07:00", out: "2006-01-02T15:04:05-07:00"}, // {in: "2009-08-12T22:15:09-07:00", out: "2006-01-02T15:04:05-07:00"},
// yyyy-mm-ddThh:mm:ss-0700 // // yyyy-mm-ddThh:mm:ss-0700
{in: "2009-08-12T22:15:09-0700", out: "2006-01-02T15:04:05-0700"}, // {in: "2009-08-12T22:15:09-0700", out: "2006-01-02T15:04:05-0700"},
// yyyy-mm-ddThh:mm:ssZ // // yyyy-mm-ddThh:mm:ssZ
{in: "2009-08-12T22:15Z", out: "2006-01-02T15:04Z"}, // {in: "2009-08-12T22:15Z", out: "2006-01-02T15:04Z"},
} }
func TestParseLayout(t *testing.T) { func TestParseLayout(t *testing.T) {