Add format 02 Jan 2018 23:59 closes #38

This commit is contained in:
Aaron Raddon 2018-02-08 12:52:03 -08:00
parent 828a4abb0c
commit a7818d843a
4 changed files with 118 additions and 16 deletions

View File

@ -73,8 +73,9 @@ var examples = []string{
"Mon Aug 10 15:44:11 UTC+0100 2015", "Mon Aug 10 15:44:11 UTC+0100 2015",
"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", "12 Feb 2006 19:17",
"03 February 2013", "03 February 2013",
"2013-Feb-03",
// mm/dd/yy // mm/dd/yy
"3/31/2014", "3/31/2014",
"03/31/2014", "03/31/2014",
@ -186,8 +187,9 @@ func main() {
| Mon Aug 10 15:44:11 UTC+0100 2015 | 2015-08-10 15:44:11 +0000 UTC | | Mon Aug 10 15:44:11 UTC+0100 2015 | 2015-08-10 15:44:11 +0000 UTC |
| 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 | | 12 Feb 2006 19:17 | 2006-02-12 19:17:00 +0000 UTC |
| 03 February 2013 | 2013-02-03 00:00:00 +0000 UTC | | 03 February 2013 | 2013-02-03 00:00:00 +0000 UTC |
| 2013-Feb-03 | 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 |

View File

@ -22,8 +22,9 @@ var examples = []string{
"Mon Aug 10 15:44:11 UTC+0100 2015", "Mon Aug 10 15:44:11 UTC+0100 2015",
"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", "12 Feb 2006 19:17",
"03 February 2013", "03 February 2013",
"2013-Feb-03",
// mm/dd/yy // mm/dd/yy
"3/31/2014", "3/31/2014",
"03/31/2014", "03/31/2014",
@ -135,8 +136,9 @@ func main() {
| Mon Aug 10 15:44:11 UTC+0100 2015 | 2015-08-10 15:44:11 +0000 UTC | | Mon Aug 10 15:44:11 UTC+0100 2015 | 2015-08-10 15:44:11 +0000 UTC |
| 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 | | 12 Feb 2006 19:17 | 2006-02-12 19:17:00 +0000 UTC |
| 03 February 2013 | 2013-02-03 00:00:00 +0000 UTC | | 03 February 2013 | 2013-02-03 00:00:00 +0000 UTC |
| 2013-Feb-03 | 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 |

View File

@ -47,7 +47,10 @@ const (
stateDigitChineseYear stateDigitChineseYear
stateDigitChineseYearWs stateDigitChineseYearWs
stateDigitWs stateDigitWs
stateDigitWsAlphaComma stateDigitWsMoShort
stateDigitWsMoShortColon
stateDigitWsMoShortColonColon
stateDigitWsMoShortComma
stateAlpha stateAlpha
stateAlphaWS stateAlphaWS
stateAlphaWSDigitComma stateAlphaWSDigitComma
@ -425,10 +428,43 @@ iterRunes:
case stateDigitWs: case stateDigitWs:
// 18 January 2018 // 18 January 2018
// 8 January 2018 // 8 January 2018
//stateDigitWsMoShort
// 02 Jan 2018 23:59
// 02 Jan 2018 23:59:34
// stateDigitWsMoShortComma
// 12 Feb 2006, 19:17 // 12 Feb 2006, 19:17
// 12 Feb 2006, 19:17:22 // 12 Feb 2006, 19:17:22
if r == ',' { if r == ' ' {
state = stateDigitWsAlphaComma if i <= part1Len+len(" Feb") {
state = stateDigitWsMoShort
} else {
break iterRunes
}
}
case stateDigitWsMoShort:
// 18 January 2018
// 8 January 2018
// stateDigitWsMoShort
// stateDigitWsMoShortColon
// 02 Jan 2018 23:59
// stateDigitWsMoShortComma
// 12 Feb 2006, 19:17
// 12 Feb 2006, 19:17:22
switch r {
case ':':
state = stateDigitWsMoShortColon
case ',':
state = stateDigitWsMoShortComma
}
case stateDigitWsMoShortColon:
// 02 Jan 2018 23:59
// stateDigitWsMoShortColonColon
// 02 Jan 2018 23:59:45
switch r {
case ':':
state = stateDigitWsMoShortColonColon
break iterRunes
} }
case stateDigitChineseYear: case stateDigitChineseYear:
@ -965,7 +1001,6 @@ iterRunes:
} }
} }
case stateDigitDashWsWsOffsetColonAlpha: case stateDigitDashWsWsOffsetColonAlpha:
// 2015-02-18 00:12:00 +00:00 UTC // 2015-02-18 00:12:00 +00:00 UTC
for _, layout := range []string{ for _, layout := range []string{
@ -1057,7 +1092,6 @@ iterRunes:
} }
} }
if len(datestr) > len("2006-01-02 03:04:05") { if len(datestr) > len("2006-01-02 03:04:05") {
t, err := parse("2006-01-02 03:04:05", datestr[:len("2006-01-02 03:04:05")], loc) t, err := parse("2006-01-02 03:04:05", datestr[:len("2006-01-02 03:04:05")], loc)
if err == nil { if err == nil {
@ -1178,8 +1212,6 @@ iterRunes:
} }
case stateDigitDotDot: case stateDigitDotDot:
//u.Infof("%s lty?%v", datestr, len(datestr)-part2Len)
switch { switch {
case len(datestr) == len("01.02.2006"): case len(datestr) == len("01.02.2006"):
return parse("01.02.2006", datestr, loc) return parse("01.02.2006", datestr, loc)
@ -1204,7 +1236,55 @@ iterRunes:
return parse("2 January 2006", datestr, loc) return parse("2 January 2006", datestr, loc)
} }
return parse("02 January 2006", datestr, loc) return parse("02 January 2006", datestr, loc)
case stateDigitWsAlphaComma: // 02 Jan 2018 23:59
case stateDigitWsMoShortColon:
// 2 Jan 2018 23:59
// 02 Jan 2018 23:59
if part1Len == 1 {
for _, layout := range []string{
"2 Jan 2006 15:04",
"2 Jan 2006 15:4",
} {
if t, err := parse(layout, datestr, loc); err == nil {
return t, nil
}
}
}
for _, layout := range []string{
"02 Jan 2006 15:04",
"02 Jan 2006 15:4",
} {
if t, err := parse(layout, datestr, loc); err == nil {
return t, nil
}
}
case stateDigitWsMoShortColonColon:
// 02 Jan 2018 23:59:45
if part1Len == 1 {
for _, layout := range []string{
"2 Jan 2006 15:04:05",
"2 Jan 2006 15:04:5",
"2 Jan 2006 15:4:5",
"2 Jan 2006 15:4:05",
} {
if t, err := parse(layout, datestr, loc); err == nil {
return t, nil
}
}
}
for _, layout := range []string{
"2 Jan 2006 15:04:05",
"2 Jan 2006 15:04:5",
"2 Jan 2006 15:4:5",
"2 Jan 2006 15:4:05",
} {
if t, err := parse(layout, datestr, loc); err == nil {
return t, nil
}
}
case stateDigitWsMoShortComma:
// 12 Feb 2006, 19:17 // 12 Feb 2006, 19:17
// 12 Feb 2006, 19:17:22 // 12 Feb 2006, 19:17:22
for _, layout := range []string{ for _, layout := range []string{

View File

@ -195,9 +195,27 @@ func TestParse(t *testing.T) {
ts = MustParse("12 Feb 2006, 19:17") ts = MustParse("12 Feb 2006, 19:17")
assert.Equal(t, "2006-02-12 19:17:00 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC))) assert.Equal(t, "2006-02-12 19:17:00 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))
ts = MustParse("2 Feb 2006, 19:17")
assert.Equal(t, "2006-02-02 19:17:00 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))
ts = MustParse("12 Feb 2006, 19:17:22") ts = MustParse("12 Feb 2006, 19:17:22")
assert.Equal(t, "2006-02-12 19:17:22 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC))) assert.Equal(t, "2006-02-12 19:17:22 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))
ts = MustParse("2 Feb 2006, 19:17:22")
assert.Equal(t, "2006-02-02 19:17:22 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))
ts = MustParse("12 Feb 2006 19:17")
assert.Equal(t, "2006-02-12 19:17:00 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))
ts = MustParse("2 Feb 2006 19:17")
assert.Equal(t, "2006-02-02 19:17:00 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))
ts = MustParse("12 Feb 2006 19:17:22")
assert.Equal(t, "2006-02-12 19:17:22 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))
ts = MustParse("2 Feb 2006 19:17:22")
assert.Equal(t, "2006-02-02 19:17:22 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))
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)))