From a7818d843a34b9e016df53ae44cc650fec1f2ab1 Mon Sep 17 00:00:00 2001 From: Aaron Raddon Date: Thu, 8 Feb 2018 12:52:03 -0800 Subject: [PATCH] Add format 02 Jan 2018 23:59 closes #38 --- README.md | 6 ++- example/main.go | 6 ++- parseany.go | 104 +++++++++++++++++++++++++++++++++++++++++------ parseany_test.go | 18 ++++++++ 4 files changed, 118 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 028bada..a1d4bc6 100644 --- a/README.md +++ b/README.md @@ -73,8 +73,9 @@ var examples = []string{ "Mon Aug 10 15:44:11 UTC+0100 2015", "Fri Jul 03 2015 18:04:07 GMT+0100 (GMT Daylight Time)", "12 Feb 2006, 19:17", - "2013-Feb-03", + "12 Feb 2006 19:17", "03 February 2013", + "2013-Feb-03", // mm/dd/yy "3/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 | | 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 | +| 12 Feb 2006 19:17 | 2006-02-12 19:17: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 | | 03/31/2014 | 2014-03-31 00:00:00 +0000 UTC | | 08/21/71 | 1971-08-21 00:00:00 +0000 UTC | diff --git a/example/main.go b/example/main.go index 172477d..fd70f0a 100644 --- a/example/main.go +++ b/example/main.go @@ -22,8 +22,9 @@ var examples = []string{ "Mon Aug 10 15:44:11 UTC+0100 2015", "Fri Jul 03 2015 18:04:07 GMT+0100 (GMT Daylight Time)", "12 Feb 2006, 19:17", - "2013-Feb-03", + "12 Feb 2006 19:17", "03 February 2013", + "2013-Feb-03", // mm/dd/yy "3/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 | | 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 | +| 12 Feb 2006 19:17 | 2006-02-12 19:17: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 | | 03/31/2014 | 2014-03-31 00:00:00 +0000 UTC | | 08/21/71 | 1971-08-21 00:00:00 +0000 UTC | diff --git a/parseany.go b/parseany.go index 41ad356..a31ae54 100644 --- a/parseany.go +++ b/parseany.go @@ -47,7 +47,10 @@ const ( stateDigitChineseYear stateDigitChineseYearWs stateDigitWs - stateDigitWsAlphaComma + stateDigitWsMoShort + stateDigitWsMoShortColon + stateDigitWsMoShortColonColon + stateDigitWsMoShortComma stateAlpha stateAlphaWS stateAlphaWSDigitComma @@ -425,10 +428,43 @@ iterRunes: case stateDigitWs: // 18 January 2018 // 8 January 2018 - // 12 Feb 2006, 19:17 - // 12 Feb 2006, 19:17:22 - if r == ',' { - state = stateDigitWsAlphaComma + //stateDigitWsMoShort + // 02 Jan 2018 23:59 + // 02 Jan 2018 23:59:34 + // stateDigitWsMoShortComma + // 12 Feb 2006, 19:17 + // 12 Feb 2006, 19:17:22 + if r == ' ' { + 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: @@ -965,7 +1001,6 @@ iterRunes: } } - case stateDigitDashWsWsOffsetColonAlpha: // 2015-02-18 00:12:00 +00:00 UTC for _, layout := range []string{ @@ -1057,7 +1092,6 @@ iterRunes: } } - 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) if err == nil { @@ -1178,8 +1212,6 @@ iterRunes: } case stateDigitDotDot: - - //u.Infof("%s lty?%v", datestr, len(datestr)-part2Len) switch { case len(datestr) == len("01.02.2006"): return parse("01.02.2006", datestr, loc) @@ -1204,7 +1236,55 @@ iterRunes: return parse("2 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:22 for _, layout := range []string{ @@ -1280,7 +1360,7 @@ iterRunes: } } } else { - for _, layout := range []string{"01/02/2006 15:4", "01/2/2006 15:4", "1/02/2006 15:4", "1/2/2006 15:4", "1/2/06 15:4", "01/02/06 15:4","01/02/2006 15:04", "01/2/2006 15:04", "1/02/2006 15:04", "1/2/2006 15:04", "1/2/06 15:04", "01/02/06 15:04"} { + for _, layout := range []string{"01/02/2006 15:4", "01/2/2006 15:4", "1/02/2006 15:4", "1/2/2006 15:4", "1/2/06 15:4", "01/02/06 15:4", "01/02/2006 15:04", "01/2/2006 15:04", "1/02/2006 15:04", "1/2/2006 15:04", "1/2/06 15:04", "01/02/06 15:04"} { if t, err := parse(layout, datestr, loc); err == nil { return t, nil } @@ -1402,7 +1482,7 @@ iterRunes: "Mon, _2 Jan 2006 15:04:5 MST", "Mon, _2 Jan 2006 15:4:5 MST", "Mon, _2 Jan 2006 15:4:05 MST", - } { + } { if t, err := parse(layout, datestr, loc); err == nil { return t, nil } diff --git a/parseany_test.go b/parseany_test.go index 4374e7c..4cb400c 100644 --- a/parseany_test.go +++ b/parseany_test.go @@ -195,9 +195,27 @@ func TestParse(t *testing.T) { 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("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") assert.Equal(t, "2013-02-03 00:00:00 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))