diff --git a/README.md b/README.md index 6b4ced0..7ab8cd0 100644 --- a/README.md +++ b/README.md @@ -127,7 +127,9 @@ var examples = []string{ "2006-01-02T15:04:05+0000", "2009-08-12T22:15:09-07:00", "2009-08-12T22:15:09", + "2009-08-12T22:15:09.988", "2009-08-12T22:15:09Z", + "2017-07-19T03:21:51:897+0100", // yyyy-mm-dd hh:mm:ss "2014-04-26 17:24:37.3186369", "2012-08-03 18:31:59.257000000", @@ -273,7 +275,9 @@ func main() { | 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 | +| 2009-08-12T22:15:09.988 | 2009-08-12 22:15:09.988 +0000 UTC | | 2009-08-12T22:15:09Z | 2009-08-12 22:15:09 +0000 UTC | +| 2017-07-19T03:21:51:897+0100 | 2017-07-19 03:21:51.897 +0100 +0100 | | 2014-04-26 17:24:37.3186369 | 2014-04-26 17:24:37.3186369 +0000 UTC | | 2012-08-03 18:31:59.257000000 | 2012-08-03 18:31:59.257 +0000 UTC | | 2014-04-26 17:24:37.123 | 2014-04-26 17:24:37.123 +0000 UTC | diff --git a/example/main.go b/example/main.go index f7b572f..0cfc745 100644 --- a/example/main.go +++ b/example/main.go @@ -84,7 +84,9 @@ var examples = []string{ "2006-01-02T15:04:05+0000", "2009-08-12T22:15:09-07:00", "2009-08-12T22:15:09", + "2009-08-12T22:15:09.988", "2009-08-12T22:15:09Z", + "2017-07-19T03:21:51:897+0100", // yyyy-mm-dd hh:mm:ss "2014-04-26 17:24:37.3186369", "2012-08-03 18:31:59.257000000", @@ -230,7 +232,9 @@ func main() { | 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 | +| 2009-08-12T22:15:09.988 | 2009-08-12 22:15:09.988 +0000 UTC | | 2009-08-12T22:15:09Z | 2009-08-12 22:15:09 +0000 UTC | +| 2017-07-19T03:21:51:897+0100 | 2017-07-19 03:21:51.897 +0100 +0100 | | 2014-04-26 17:24:37.3186369 | 2014-04-26 17:24:37.3186369 +0000 UTC | | 2012-08-03 18:31:59.257000000 | 2012-08-03 18:31:59.257 +0000 UTC | | 2014-04-26 17:24:37.123 | 2014-04-26 17:24:37.123 +0000 UTC | diff --git a/parseany.go b/parseany.go index b2f9ac0..d803e43 100644 --- a/parseany.go +++ b/parseany.go @@ -412,6 +412,7 @@ iterRunes: case dateYearDash: // dateYearDashDashT // 2006-01-02T15:04:05Z07:00 + // 2020-08-17T17:00:00:000+0100 // dateYearDashDashWs // 2013-04-01 22:43:22 // dateYearDashAlphaDash @@ -447,6 +448,12 @@ iterRunes: p.setDay() break iterRunes } + + case dateYearDashDashT: + // dateYearDashDashT + // 2006-01-02T15:04:05Z07:00 + // 2020-08-17T17:00:00:000+0100 + case dateYearDashAlphaDash: // 2013-Feb-03 switch r { @@ -1105,7 +1112,7 @@ iterRunes: for ; i < len(datestr); i++ { r := rune(datestr[i]) - // gou.Debugf("%d %s %d iterTimeRunes %s %s", i, string(r), p.stateTime, p.ds(), p.ts()) + // gou.Debugf("i=%d r=%s state=%d iterTimeRunes %s %s", i, string(r), p.stateTime, p.ds(), p.ts()) switch p.stateTime { case timeStart: @@ -1162,7 +1169,12 @@ iterRunes: // 22:18+0530 p.minlen = i - p.mini } else { - p.seclen = i - p.seci + if p.seclen == 0 { + p.seclen = i - p.seci + } + if p.msi > 0 && p.mslen == 0 { + p.mslen = i - p.msi + } } p.offseti = i case '.': @@ -1220,6 +1232,19 @@ iterRunes: } else if p.seci == 0 { p.seci = i + 1 p.minlen = i - p.mini + } else if p.seci > 0 { + // 18:31:59:257 ms uses colon, wtf + p.seclen = i - p.seci + p.set(p.seci, "05") + p.msi = i + 1 + + // gross, gross, gross. manipulating the datestr is horrible. + // https://github.com/araddon/dateparse/issues/117 + // Could not get the parsing to work using golang time.Parse() without + // replacing that colon with period. + p.set(i, ".") + datestr = datestr[0:i] + "." + datestr[i+1:] + p.datestr = datestr } } case timeOffset: @@ -2079,7 +2104,7 @@ func (p *parser) parse() (time.Time, error) { p.datestr = p.datestr[p.skip:] } - //gou.Debugf("parse %q AS %q", p.datestr, string(p.format)) + // gou.Debugf("parse %q AS %q", p.datestr, string(p.format)) if p.loc == nil { return time.Parse(string(p.format), p.datestr) } diff --git a/parseany_test.go b/parseany_test.go index 1aa67ee..06357e2 100644 --- a/parseany_test.go +++ b/parseany_test.go @@ -11,8 +11,8 @@ import ( func TestOne(t *testing.T) { time.Local = time.UTC var ts time.Time - ts = MustParse("Sun, 3 Jan 2021 00:12:23 +0800 (GMT+08:00)") - assert.Equal(t, "2021-01-02 16:12:23 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC))) + ts = MustParse("2020-08-17T17:00:00:987+0100") + assert.Equal(t, "2020-08-17 16:00:00.987 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC))) } type dateTest struct { @@ -376,6 +376,8 @@ var testInputs = []dateTest{ {in: "2016-06-21T19:55:00.799+0100", out: "2016-06-21 18:55:00.799 +0000 UTC"}, {in: "2016-06-21T19:55+0100", out: "2016-06-21 18:55:00 +0000 UTC"}, {in: "2016-06-21T19:55+0130", out: "2016-06-21 18:25:00 +0000 UTC"}, + // yyyy-mm-ddThh:mm:ss:000+0000 + {in: "2012-08-17T18:31:59.257+0100", out: "2012-08-17 17:31:59.257 +0000 UTC"}, // https://github.com/araddon/dateparse/issues/117 // yyyy-mm-ddThh:mm:ssZ {in: "2009-08-12T22:15Z", out: "2009-08-12 22:15:00 +0000 UTC"}, {in: "2009-08-12T22:15:09Z", out: "2009-08-12 22:15:09 +0000 UTC"},