Add colon date format mm:dd:yy hh:mm:ss

This commit is contained in:
asannikov 2020-06-17 22:55:25 +02:00
parent d820a6159a
commit 9e5141594f
3 changed files with 91 additions and 6 deletions

View File

@ -106,6 +106,15 @@ var examples = []string{
"2014/4/02 03:00:51",
"2012/03/19 10:11:59",
"2012/03/19 10:11:59.3186369",
// yyyy:mm:dd
"2014:3:31",
"2014:03:31",
"2014:4:8 22:05",
"2014:04:08 22:05",
"2014:04:2 03:00:51",
"2014:4:02 03:00:51",
"2012:03:19 10:11:59",
"2012:03:19 10:11:59.3186369",
// Chinese
"2014年04月08日",
// yyyy-mm-ddThh

View File

@ -49,23 +49,24 @@ const (
dateDigitDot // 10
dateDigitDotDot
dateDigitSlash
dateDigitColon
dateDigitChineseYear
dateDigitChineseYearWs
dateDigitWs // 15
dateDigitChineseYearWs // 15
dateDigitWs
dateDigitWsMoYear
dateDigitWsMolong
dateAlpha
dateAlphaWs
dateAlphaWsDigit // 20
dateAlphaWs // 20
dateAlphaWsDigit
dateAlphaWsDigitMore
dateAlphaWsDigitMoreWs
dateAlphaWsDigitMoreWsYear
dateAlphaWsMonth
dateAlphaWsMonth // 25
dateAlphaWsMonthMore
dateAlphaWsMonthSuffix
dateAlphaWsMore
dateAlphaWsAtTime
dateAlphaWsAlpha
dateAlphaWsAlpha // 30
dateAlphaWsAlphaYearmaybe
dateAlphaPeriodWsDigit
dateWeekdayComma
@ -272,6 +273,25 @@ iterRunes:
}
}
case ':':
// 03/31/2005
// 2014/02/24
p.stateDate = dateDigitColon
if i == 4 {
p.yearlen = i
p.moi = i + 1
p.setYear()
} else {
p.ambiguousMD = true
if p.preferMonthFirst {
if p.molen == 0 {
p.molen = i
p.setMonth()
p.dayi = i + 1
}
}
}
case '.':
// 3.31.2014
// 08.21.71
@ -449,6 +469,45 @@ iterRunes:
}
}
case dateDigitColon:
// 2014:07:10 06:55:38.156283
// 03:19:2012 10:11:59
// 04:2:2014 03:00:37
// 3:1:2012 10:11:59
// 4:8:2014 22:05
// 3:1:2014
// 10:13:2014
// 01:02:2006
// 1:2:06
switch r {
case ' ':
p.stateTime = timeStart
if p.yearlen == 0 {
p.yearlen = i - p.yeari
p.setYear()
} else if p.daylen == 0 {
p.daylen = i - p.dayi
p.setDay()
}
break iterRunes
case ':':
if p.yearlen > 0 {
// 2014:07:10 06:55:38.156283
if p.molen == 0 {
p.molen = i - p.moi
p.setMonth()
p.dayi = i + 1
}
} else if p.preferMonthFirst {
if p.daylen == 0 {
p.daylen = i - p.dayi
p.setDay()
p.yeari = i + 1
}
}
}
case dateDigitWs:
// 18 January 2018
// 8 January 2018
@ -1640,6 +1699,13 @@ iterRunes:
// 2014/10/13
return p, nil
case dateDigitColon:
// 3:1:2014
// 10:13:2014
// 01:02:2006
// 2014:10:13
return p, nil
case dateDigitChineseYear:
// dateDigitChineseYear
// 2014年04月08日

View File

@ -181,6 +181,16 @@ var testInputs = []dateTest{
{in: "04/02/2014 04:08:09.123", out: "2014-04-02 04:08:09.123 +0000 UTC"},
{in: "04/02/2014 04:08:09.12312", out: "2014-04-02 04:08:09.12312 +0000 UTC"},
{in: "04/02/2014 04:08:09.123123", out: "2014-04-02 04:08:09.123123 +0000 UTC"},
// mm:dd:yy hh:mm:ss
{in: "04:02:2014 04:08:09", out: "2014-04-02 04:08:09 +0000 UTC"},
{in: "4:2:2014 04:08:09", out: "2014-04-02 04:08:09 +0000 UTC"},
{in: "04:02:2014 4:08:09", out: "2014-04-02 04:08:09 +0000 UTC"},
{in: "04:02:2014 4:8:9", out: "2014-04-02 04:08:09 +0000 UTC"},
{in: "04:02:2014 04:08", out: "2014-04-02 04:08:00 +0000 UTC"},
{in: "04:02:2014 4:8", out: "2014-04-02 04:08:00 +0000 UTC"},
{in: "04:02:2014 04:08:09.123", out: "2014-04-02 04:08:09.123 +0000 UTC"},
{in: "04:02:2014 04:08:09.12312", out: "2014-04-02 04:08:09.12312 +0000 UTC"},
{in: "04:02:2014 04:08:09.123123", out: "2014-04-02 04:08:09.123123 +0000 UTC"},
// mm/dd/yy hh:mm:ss AM
{in: "04/02/2014 04:08:09 AM", out: "2014-04-02 04:08:09 +0000 UTC"},
{in: "04/02/2014 04:08:09 PM", out: "2014-04-02 16:08:09 +0000 UTC"},