mirror of
https://github.com/araddon/dateparse.git
synced 2024-11-10 11:51:54 +08:00
new format "2 January 2006" closes #36
This commit is contained in:
parent
afa6a0f934
commit
f912f7e43f
@ -74,6 +74,7 @@ var examples = []string{
|
|||||||
"Fri Jul 03 2015 18:04:07 GMT+0100 (GMT Daylight Time)",
|
"Fri Jul 03 2015 18:04:07 GMT+0100 (GMT Daylight Time)",
|
||||||
"12 Feb 2006, 19:17",
|
"12 Feb 2006, 19:17",
|
||||||
"2013-Feb-03",
|
"2013-Feb-03",
|
||||||
|
"03 February 2013",
|
||||||
// mm/dd/yy
|
// mm/dd/yy
|
||||||
"3/31/2014",
|
"3/31/2014",
|
||||||
"03/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 |
|
| 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 |
|
| 12 Feb 2006, 19:17 | 2006-02-12 19:17:00 +0000 UTC |
|
||||||
| 2013-Feb-03 | 2013-02-03 00:00: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 |
|
| 3/31/2014 | 2014-03-31 00:00:00 +0000 UTC |
|
||||||
| 03/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 |
|
| 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 |
|
| 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 | 2012-03-19 10:11:59 +0000 UTC |
|
||||||
| 2012/03/19 10:11:59.3186369 | 2012-03-19 10:11:59.3186369 +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 |
|
| 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-07:00 | 2009-08-12 22:15:09 -0700 -0700 |
|
||||||
| 2009-08-12T22:15:09 | 2009-08-12 22:15:09 +0000 UTC |
|
| 2009-08-12T22:15:09 | 2009-08-12 22:15:09 +0000 UTC |
|
||||||
|
@ -23,6 +23,7 @@ var examples = []string{
|
|||||||
"Fri Jul 03 2015 18:04:07 GMT+0100 (GMT Daylight Time)",
|
"Fri Jul 03 2015 18:04:07 GMT+0100 (GMT Daylight Time)",
|
||||||
"12 Feb 2006, 19:17",
|
"12 Feb 2006, 19:17",
|
||||||
"2013-Feb-03",
|
"2013-Feb-03",
|
||||||
|
"03 February 2013",
|
||||||
// mm/dd/yy
|
// mm/dd/yy
|
||||||
"3/31/2014",
|
"3/31/2014",
|
||||||
"03/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 |
|
| 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 |
|
| 12 Feb 2006, 19:17 | 2006-02-12 19:17:00 +0000 UTC |
|
||||||
| 2013-Feb-03 | 2013-02-03 00:00: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 |
|
| 3/31/2014 | 2014-03-31 00:00:00 +0000 UTC |
|
||||||
| 03/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 |
|
| 08/21/71 | 1971-08-21 00:00:00 +0000 UTC |
|
||||||
|
45
parseany.go
45
parseany.go
@ -46,7 +46,8 @@ const (
|
|||||||
stateDigitSlashWSColonColonAMPM
|
stateDigitSlashWSColonColonAMPM
|
||||||
stateDigitChineseYear
|
stateDigitChineseYear
|
||||||
stateDigitChineseYearWs
|
stateDigitChineseYearWs
|
||||||
stateDigitAlpha
|
stateDigitWs
|
||||||
|
stateDigitWsAlphaComma
|
||||||
stateAlpha
|
stateAlpha
|
||||||
stateAlphaWS
|
stateAlphaWS
|
||||||
stateAlphaWSDigitComma
|
stateAlphaWSDigitComma
|
||||||
@ -150,10 +151,7 @@ iterRunes:
|
|||||||
if r == '年' {
|
if r == '年' {
|
||||||
// Chinese Year
|
// Chinese Year
|
||||||
state = stateDigitChineseYear
|
state = stateDigitChineseYear
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
state = stateDigitAlpha
|
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
switch r {
|
switch r {
|
||||||
case '-', '\u2212':
|
case '-', '\u2212':
|
||||||
@ -164,7 +162,11 @@ iterRunes:
|
|||||||
case '.':
|
case '.':
|
||||||
state = stateDigitDot
|
state = stateDigitDot
|
||||||
part1Len = i
|
part1Len = i
|
||||||
|
case ' ':
|
||||||
|
state = stateDigitWs
|
||||||
|
part1Len = i
|
||||||
}
|
}
|
||||||
|
|
||||||
case stateDigitDash: // starts digit then dash 02-
|
case stateDigitDash: // starts digit then dash 02-
|
||||||
// 2006-01-02T15:04:05Z07:00
|
// 2006-01-02T15:04:05Z07:00
|
||||||
// 2017-06-25T17:46:57.45706582-07:00
|
// 2017-06-25T17:46:57.45706582-07:00
|
||||||
@ -419,6 +421,16 @@ iterRunes:
|
|||||||
case 'A', 'P':
|
case 'A', 'P':
|
||||||
state = stateDigitSlashWSColonColonAMPM
|
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:
|
case stateDigitChineseYear:
|
||||||
// stateDigitChineseYear
|
// stateDigitChineseYear
|
||||||
// 2014年04月08日
|
// 2014年04月08日
|
||||||
@ -428,15 +440,6 @@ iterRunes:
|
|||||||
state = stateDigitChineseYearWs
|
state = stateDigitChineseYearWs
|
||||||
break
|
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:
|
case stateDigitDot:
|
||||||
// 3.31.2014
|
// 3.31.2014
|
||||||
if r == '.' {
|
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:
|
case stateAlphaWSAlphaColon:
|
||||||
// Mon Jan _2 15:04:05 2006
|
// Mon Jan _2 15:04:05 2006
|
||||||
return parse(time.ANSIC, datestr, loc)
|
return parse(time.ANSIC, datestr, loc)
|
||||||
|
@ -183,6 +183,12 @@ func TestParse(t *testing.T) {
|
|||||||
ts = MustParse("2013-Feb-03")
|
ts = MustParse("2013-Feb-03")
|
||||||
assert.Equal(t, "2013-02-03 00:00:00 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))
|
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日
|
// Chinese 2014年04月18日
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user