new format "2 January 2006" closes #36

This commit is contained in:
Aaron Raddon 2018-01-24 17:59:25 -08:00
parent afa6a0f934
commit f912f7e43f
4 changed files with 43 additions and 14 deletions

View File

@ -74,6 +74,7 @@ var examples = []string{
"Fri Jul 03 2015 18:04:07 GMT+0100 (GMT Daylight Time)",
"12 Feb 2006, 19:17",
"2013-Feb-03",
"03 February 2013",
// mm/dd/yy
"3/31/2014",
"03/31/2014",
@ -186,6 +187,7 @@ func main() {
| Fri Jul 03 2015 18:04:07 GMT+0100 (GMT Daylight Time) | 2015-07-03 18:04:07 +0100 GMT |
| 12 Feb 2006, 19:17 | 2006-02-12 19:17:00 +0000 UTC |
| 2013-Feb-03 | 2013-02-03 00:00:00 +0000 UTC |
| 03 February 2013 | 2013-02-03 00:00:00 +0000 UTC |
| 3/31/2014 | 2014-03-31 00:00:00 +0000 UTC |
| 03/31/2014 | 2014-03-31 00:00:00 +0000 UTC |
| 08/21/71 | 1971-08-21 00:00:00 +0000 UTC |
@ -210,7 +212,7 @@ func main() {
| 2014/4/02 03:00:51 | 2014-04-02 03:00:51 +0000 UTC |
| 2012/03/19 10:11:59 | 2012-03-19 10:11:59 +0000 UTC |
| 2012/03/19 10:11:59.3186369 | 2012-03-19 10:11:59.3186369 +0000 UTC |
| 2014年04月08日 | 2014-04-08 00:00:00 +0000 UTC |
| 2014年04月08日 | 2014-04-08 00:00:00 +0000 UTC |
| 2006-01-02T15:04:05+0000 | 2006-01-02 15:04:05 +0000 UTC |
| 2009-08-12T22:15:09-07:00 | 2009-08-12 22:15:09 -0700 -0700 |
| 2009-08-12T22:15:09 | 2009-08-12 22:15:09 +0000 UTC |

View File

@ -23,6 +23,7 @@ var examples = []string{
"Fri Jul 03 2015 18:04:07 GMT+0100 (GMT Daylight Time)",
"12 Feb 2006, 19:17",
"2013-Feb-03",
"03 February 2013",
// mm/dd/yy
"3/31/2014",
"03/31/2014",
@ -135,6 +136,7 @@ func main() {
| Fri Jul 03 2015 18:04:07 GMT+0100 (GMT Daylight Time) | 2015-07-03 18:04:07 +0100 GMT |
| 12 Feb 2006, 19:17 | 2006-02-12 19:17:00 +0000 UTC |
| 2013-Feb-03 | 2013-02-03 00:00:00 +0000 UTC |
| 03 February 2013 | 2013-02-03 00:00:00 +0000 UTC |
| 3/31/2014 | 2014-03-31 00:00:00 +0000 UTC |
| 03/31/2014 | 2014-03-31 00:00:00 +0000 UTC |
| 08/21/71 | 1971-08-21 00:00:00 +0000 UTC |

View File

@ -46,7 +46,8 @@ const (
stateDigitSlashWSColonColonAMPM
stateDigitChineseYear
stateDigitChineseYearWs
stateDigitAlpha
stateDigitWs
stateDigitWsAlphaComma
stateAlpha
stateAlphaWS
stateAlphaWSDigitComma
@ -150,10 +151,7 @@ iterRunes:
if r == '年' {
// Chinese Year
state = stateDigitChineseYear
continue
}
state = stateDigitAlpha
continue
}
switch r {
case '-', '\u2212':
@ -164,7 +162,11 @@ iterRunes:
case '.':
state = stateDigitDot
part1Len = i
case ' ':
state = stateDigitWs
part1Len = i
}
case stateDigitDash: // starts digit then dash 02-
// 2006-01-02T15:04:05Z07:00
// 2017-06-25T17:46:57.45706582-07:00
@ -419,6 +421,16 @@ iterRunes:
case 'A', 'P':
state = stateDigitSlashWSColonColonAMPM
}
case stateDigitWs:
// 18 January 2018
// 8 January 2018
// 12 Feb 2006, 19:17
// 12 Feb 2006, 19:17:22
if r == ',' {
state = stateDigitWsAlphaComma
}
case stateDigitChineseYear:
// stateDigitChineseYear
// 2014年04月08日
@ -428,15 +440,6 @@ iterRunes:
state = stateDigitChineseYearWs
break
}
case stateDigitAlpha:
// 12 Feb 2006, 19:17
// 12 Feb 2006, 19:17:22
switch {
case len(datestr) == len("02 Jan 2006, 15:04"):
return parse("02 Jan 2006, 15:04", datestr, loc)
case len(datestr) == len("02 Jan 2006, 15:04:05"):
return parse("02 Jan 2006, 15:04:05", datestr, loc)
}
case stateDigitDot:
// 3.31.2014
if r == '.' {
@ -781,6 +784,22 @@ iterRunes:
}
}
case stateDigitWs:
// 18 January 2018
// 8 January 2018
if part1Len == 1 {
return parse("2 January 2006", datestr, loc)
}
return parse("02 January 2006", datestr, loc)
case stateDigitWsAlphaComma:
// 12 Feb 2006, 19:17
// 12 Feb 2006, 19:17:22
switch {
case len(datestr) == len("02 Jan 2006, 15:04"):
return parse("02 Jan 2006, 15:04", datestr, loc)
case len(datestr) == len("02 Jan 2006, 15:04:05"):
return parse("02 Jan 2006, 15:04:05", datestr, loc)
}
case stateAlphaWSAlphaColon:
// Mon Jan _2 15:04:05 2006
return parse(time.ANSIC, datestr, loc)

View File

@ -183,6 +183,12 @@ func TestParse(t *testing.T) {
ts = MustParse("2013-Feb-03")
assert.Equal(t, "2013-02-03 00:00:00 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))
ts = MustParse("03 February 2013")
assert.Equal(t, "2013-02-03 00:00:00 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))
ts = MustParse("3 February 2013")
assert.Equal(t, "2013-02-03 00:00:00 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))
//---------------------------------------------
// Chinese 2014年04月18日