diff --git a/README.md b/README.md index a1d4bc6..26cbe7c 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,8 @@ import ( var examples = []string{ "May 8, 2009 5:57:51 PM", + "oct 7, 1970", + "oct 7, '70", "Mon Jan 2 15:04:05 2006", "Mon Jan 2 15:04:05 MST 2006", "Mon Jan 02 15:04:05 -0700 2006", @@ -74,6 +76,8 @@ var examples = []string{ "Fri Jul 03 2015 18:04:07 GMT+0100 (GMT Daylight Time)", "12 Feb 2006, 19:17", "12 Feb 2006 19:17", + "7 oct 70", + "7 oct 1970", "03 February 2013", "2013-Feb-03", // mm/dd/yy @@ -176,6 +180,8 @@ func main() { | Input | Parsed, and Output as %v | +-------------------------------------------------------+----------------------------------------+ | May 8, 2009 5:57:51 PM | 2009-05-08 17:57:51 +0000 UTC | +| oct 7, 1970 | 1970-10-07 00:00:00 +0000 UTC | +| oct 7, '70 | 1970-10-07 00:00:00 +0000 UTC | | Mon Jan 2 15:04:05 2006 | 2006-01-02 15:04:05 +0000 UTC | | Mon Jan 2 15:04:05 MST 2006 | 2006-01-02 15:04:05 +0000 MST | | Mon Jan 02 15:04:05 -0700 2006 | 2006-01-02 15:04:05 -0700 -0700 | @@ -188,6 +194,8 @@ 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 | | 12 Feb 2006 19:17 | 2006-02-12 19:17:00 +0000 UTC | +| 7 oct 70 | 1970-10-07 00:00:00 +0000 UTC | +| 7 oct 1970 | 1970-10-07 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 | @@ -228,7 +236,7 @@ func main() { | 2014-12-16 06:20:00 GMT | 2014-12-16 06:20:00 +0000 UTC | | 2014-04-26 05:24:37 PM | 2014-04-26 17:24:37 +0000 UTC | | 2014-04-26 13:13:43 +0800 | 2014-04-26 13:13:43 +0800 +0800 | -| 2014-04-26 13:13:44 +09:00 | 2014-04-26 13:13:44 +0900 +0900 | +| 2014-04-26 13:13:44 +09:00 | 2014-04-26 13:13:44 +0000 UTC | | 2012-08-03 18:31:59.257000000 +0000 UTC | 2012-08-03 18:31:59.257 +0000 UTC | | 2015-09-30 18:48:56.35272715 +0000 UTC | 2015-09-30 18:48:56.35272715 +0000 UTC | | 2015-02-18 00:12:00 +0000 GMT | 2015-02-18 00:12:00 +0000 UTC | diff --git a/example/main.go b/example/main.go index fd70f0a..4e10c9e 100644 --- a/example/main.go +++ b/example/main.go @@ -11,6 +11,8 @@ import ( var examples = []string{ "May 8, 2009 5:57:51 PM", + "oct 7, 1970", + "oct 7, '70", "Mon Jan 2 15:04:05 2006", "Mon Jan 2 15:04:05 MST 2006", "Mon Jan 02 15:04:05 -0700 2006", @@ -23,6 +25,8 @@ var examples = []string{ "Fri Jul 03 2015 18:04:07 GMT+0100 (GMT Daylight Time)", "12 Feb 2006, 19:17", "12 Feb 2006 19:17", + "7 oct 70", + "7 oct 1970", "03 February 2013", "2013-Feb-03", // mm/dd/yy @@ -125,6 +129,8 @@ func main() { | Input | Parsed, and Output as %v | +-------------------------------------------------------+----------------------------------------+ | May 8, 2009 5:57:51 PM | 2009-05-08 17:57:51 +0000 UTC | +| oct 7, 1970 | 1970-10-07 00:00:00 +0000 UTC | +| oct 7, '70 | 1970-10-07 00:00:00 +0000 UTC | | Mon Jan 2 15:04:05 2006 | 2006-01-02 15:04:05 +0000 UTC | | Mon Jan 2 15:04:05 MST 2006 | 2006-01-02 15:04:05 +0000 MST | | Mon Jan 02 15:04:05 -0700 2006 | 2006-01-02 15:04:05 -0700 -0700 | @@ -137,6 +143,8 @@ 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 | | 12 Feb 2006 19:17 | 2006-02-12 19:17:00 +0000 UTC | +| 7 oct 70 | 1970-10-07 00:00:00 +0000 UTC | +| 7 oct 1970 | 1970-10-07 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 | @@ -177,7 +185,7 @@ func main() { | 2014-12-16 06:20:00 GMT | 2014-12-16 06:20:00 +0000 UTC | | 2014-04-26 05:24:37 PM | 2014-04-26 17:24:37 +0000 UTC | | 2014-04-26 13:13:43 +0800 | 2014-04-26 13:13:43 +0800 +0800 | -| 2014-04-26 13:13:44 +09:00 | 2014-04-26 13:13:44 +0900 +0900 | +| 2014-04-26 13:13:44 +09:00 | 2014-04-26 13:13:44 +0000 UTC | | 2012-08-03 18:31:59.257000000 +0000 UTC | 2012-08-03 18:31:59.257 +0000 UTC | | 2015-09-30 18:48:56.35272715 +0000 UTC | 2015-09-30 18:48:56.35272715 +0000 UTC | | 2015-02-18 00:12:00 +0000 GMT | 2015-02-18 00:12:00 +0000 UTC | diff --git a/parseany.go b/parseany.go index 60343a6..eaec7d0 100644 --- a/parseany.go +++ b/parseany.go @@ -278,6 +278,7 @@ func (p *parser) parse() (time.Time, error) { p.format = p.format[p.skip:] p.datestr = p.datestr[p.skip:] } + //gou.Debugf("parse %q AS %s", p.datestr, string(p.format)) if p.loc == nil { return time.Parse(string(p.format), p.datestr) } @@ -350,6 +351,7 @@ iterRunes: case ' ': // 18 January 2018 // 8 January 2018 + // 8 jan 2018 // 02 Jan 2018 23:59 // 02 Jan 2018 23:59:34 // 12 Feb 2006, 19:17 @@ -486,6 +488,8 @@ iterRunes: case dateDigitWs: // 18 January 2018 // 8 January 2018 + // 8 jan 2018 + // 1 jan 18 // 02 Jan 2018 23:59 // 02 Jan 2018 23:59:34 // 12 Feb 2006, 19:17 @@ -493,7 +497,7 @@ iterRunes: switch r { case ' ': p.yeari = i + 1 - p.yearlen = 4 + //p.yearlen = 4 p.dayi = 0 p.daylen = p.part1Len p.setDay() @@ -509,6 +513,7 @@ iterRunes: } case dateDigitWsMoYear: + // 8 jan 2018 // 02 Jan 2018 23:59 // 02 Jan 2018 23:59:34 // 12 Feb 2006, 19:17 @@ -552,6 +557,7 @@ iterRunes: // Fri Jul 03 2015 18:04:07 GMT+0100 (GMT Daylight Time) // dateAlphaWSDigit // May 8, 2009 5:57:51 PM + // oct 1, 1970 // // dateWeekdayComma // Monday, 02 Jan 2006 15:04:05 MST @@ -652,6 +658,8 @@ iterRunes: // Mon Aug 10 15:44:11 UTC+0100 2015 // dateAlphaWsDigit // May 8, 2009 5:57:51 PM + // oct 1, 1970 + // oct 7, '70 switch { case unicode.IsLetter(r): p.set(0, "Mon") @@ -665,24 +673,33 @@ iterRunes: } case dateAlphaWsDigit: - // dateAlphaWsDigit - // May 8, 2009 5:57:51 PM + // May 8, 2009 5:57:51 PM + // oct 1, 1970 + // oct 7, '70 + //gou.Debugf("%d %s dateAlphaWsDigit %s %s", i, string(r), p.ds(), p.ts()) if r == ',' { p.daylen = i - p.dayi p.setDay() p.stateDate = dateAlphaWsDigitComma } case dateAlphaWsDigitComma: - // x - // May 8, 2009 5:57:51 PM + // x + // May 8, 2009 5:57:51 PM + // oct 1, 1970 + // oct 7, '70 if r == ' ' { p.stateDate = dateAlphaWsDigitCommaWs p.yeari = i + 1 } case dateAlphaWsDigitCommaWs: - // x - // May 8, 2009 5:57:51 PM - if r == ' ' { + // x + // May 8, 2009 5:57:51 PM + // oct 1, 1970 + // oct 7, '70 + switch r { + case '\'': + p.yeari = i + 1 + case ' ': p.stateDate = dateAlphaWsDigitCommaWsYear p.yearlen = i - p.yeari p.setYear() @@ -732,7 +749,9 @@ iterRunes: p.coalesceDate(i) if p.stateTime == timeStart { // increment first one, since the i++ occurs at end of loop - i++ + if i < len(p.datestr) { + i++ + } iterTimeRunes: for ; i < len(datestr); i++ { @@ -1192,6 +1211,8 @@ iterRunes: return p.parse() case dateDigitWsMoYear: + // 2 Jan 2018 + // 2 Jan 18 // 2 Jan 2018 23:59 // 02 Jan 2018 23:59 // 02 Jan 2018 23:59:45 @@ -1207,6 +1228,12 @@ iterRunes: } return parse("2 January 2006", datestr, loc) + case dateAlphaWsDigitCommaWs: + // oct 1, 1970 + p.yearlen = i - p.yeari + p.setYear() + return p.parse() + case dateAlphaWsDigitCommaWsYear: // May 8, 2009 5:57:51 PM return p.parse() diff --git a/parseany_test.go b/parseany_test.go index 7e7ac30..904f649 100644 --- a/parseany_test.go +++ b/parseany_test.go @@ -119,6 +119,18 @@ func TestOne(t *testing.T) { ts = MustParse("2014-05-11 08:20:13,787") assert.Equal(t, "2014-05-11 08:20:13.787 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC))) } + +type dateTest struct { + in, out string +} + +var testInputs = []dateTest{ + {in: "oct 7, 1970", out: "1970-10-07 00:00:00 +0000 UTC"}, + {in: "oct 7, '70", out: "1970-10-07 00:00:00 +0000 UTC"}, + {in: "7 oct 70", out: "1970-10-07 00:00:00 +0000 UTC"}, + {in: "7 oct 1970", out: "1970-10-07 00:00:00 +0000 UTC"}, +} + func TestParse(t *testing.T) { /* @@ -138,6 +150,12 @@ func TestParse(t *testing.T) { assert.Equal(t, true, testDidPanic("NOT GONNA HAPPEN")) + for _, th := range testInputs { + ts = MustParse(th.in) + got := fmt.Sprintf("%v", ts.In(time.UTC)) + assert.Equal(t, th.out, got, "Expected %q but got %q from %q", th.out, got, th.in) + } + // TODO: Is a utf8 date valid? // ts = MustParse("2014-04\u221226") // assert.Equal(t, "2014-04-26 00:00:00 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))