diff --git a/parseany.go b/parseany.go index 7378a47..dc8abc7 100644 --- a/parseany.go +++ b/parseany.go @@ -49,32 +49,32 @@ const ( ) const ( // Time state - timeIgnore timeState = iota + timeIgnore timeState = iota // 0 timeStart timeWs timeWsAlpha timeWsAlphaWs - timeWsAlphaZoneOffset + timeWsAlphaZoneOffset // 5 timeWsAlphaZoneOffsetWs timeWsAlphaZoneOffsetWsYear timeWsAlphaZoneOffsetWsExtra timeWsAMPMMaybe - timeWsAMPM + timeWsAMPM // 10 timeWsOffset - timeWsOffsetWs + timeWsOffsetWs // 12 timeWsOffsetColonAlpha timeWsOffsetColon - timeWsYear + timeWsYear // 15 timeOffset timeOffsetColon timeAlpha timePeriod - timePeriodOffset + timePeriodOffset // 20 timePeriodOffsetColon timePeriodOffsetColonWs timePeriodWs timePeriodWsAlpha - timePeriodWsOffset + timePeriodWsOffset // 25 timePeriodWsOffsetWs timePeriodWsOffsetWsAlpha timePeriodWsOffsetColon @@ -833,6 +833,7 @@ iterRunes: // 05:24:37 PM // 06:20:00 UTC // 00:12:00 +0000 UTC + // 22:18:00 +0000 UTC m=+0.000000001 // 15:04:05 -0700 // 15:04:05 -07:00 // 15:04:05 2008 @@ -854,6 +855,7 @@ iterRunes: // 00:00:00.000 +0000 // timePeriodWsOffsetAlpha // 00:07:31.945167 +0000 UTC + // 22:18:00.001 +0000 UTC m=+0.000000001 // 00:00:00.000 +0000 UTC // timePeriodWsAlpha // 06:20:00.000 UTC @@ -1054,16 +1056,27 @@ iterRunes: case timeWsOffsetWs: // 17:57:51 -0700 2009 // 00:12:00 +0000 UTC - if unicode.IsDigit(r) { + // 22:18:00.001 +0000 UTC m=+0.000000001 + + switch { + case unicode.IsDigit(r): p.yearlen = i - p.yeari + 1 if p.yearlen == 4 { p.setYear() } - } else if unicode.IsLetter(r) { + case unicode.IsLetter(r): if p.tzi == 0 { p.tzi = i } + case r == '=': + // eff you golang + if datestr[i-1] == 'm' { + p.extra = i - 2 + p.trimExtra() + break + } } + case timeWsOffsetColon: // timeWsOffsetColon // 15:04:05 -07:00 @@ -1098,6 +1111,7 @@ iterRunes: // timePeriodWsOffsetAlpha // 00:07:31.945167 +0000 UTC // 00:00:00.000 +0000 UTC + // 22:18:00.001 +0000 UTC m=+0.000000001 // timePeriodWsAlpha // 06:20:00.000 UTC switch r { @@ -1175,6 +1189,7 @@ iterRunes: // timePeriodWsOffsetAlpha // 00:07:31.945167 +0000 UTC // 00:00:00.000 +0000 UTC + // 03:02:00.001 +0300 MSK m=+0.000000001 // timePeriodWsOffsetColon // 13:31:51.999 -07:00 MST // timePeriodWsAlpha @@ -1188,10 +1203,18 @@ iterRunes: if unicode.IsLetter(r) { // 00:07:31.945167 +0000 UTC // 00:00:00.000 +0000 UTC + // 03:02:00.001 +0300 MSK m=+0.000000001 p.stateTime = timePeriodWsOffsetWsAlpha - break iterTimeRunes } } + case timePeriodWsOffsetWsAlpha: + // 03:02:00.001 +0300 MSK m=+0.000000001 + // eff you golang + if r == '=' && datestr[i-1] == 'm' { + p.extra = i - 2 + p.trimExtra() + break + } case timePeriodWsOffsetColon: // 13:31:51.999 -07:00 MST diff --git a/parseany_test.go b/parseany_test.go index cb6da71..8c911c1 100644 --- a/parseany_test.go +++ b/parseany_test.go @@ -126,8 +126,10 @@ func TestInLocation(t *testing.T) { func TestOne(t *testing.T) { time.Local = time.UTC var ts time.Time - ts = MustParse("2018-04-02T22:18+0000") - assert.Equal(t, "2018-04-02 22:18:00 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC))) + // {in: "2015-02-08 03:02:00 +0300 MSK m=+0.000000001", out: "2015-02-08 00:02:00 +0000 UTC"}, + // {in: "2015-02-08 03:02:00.001 +0300 MSK m=+0.000000001", out: "2015-02-08 00:02:00.001 +0000 UTC"}, + ts = MustParse("2015-02-08 03:02:00.001 +0300 MSK m=+0.000000001") + assert.Equal(t, "2015-02-08 00:02:00.001 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC))) } type dateTest struct { @@ -347,6 +349,9 @@ var testInputs = []dateTest{ {in: "2014-04-26 17:24:37.123456 +0000 UTC", out: "2014-04-26 17:24:37.123456 +0000 UTC"}, {in: "2014-04-26 17:24:37.12 +0000 UTC", out: "2014-04-26 17:24:37.12 +0000 UTC"}, {in: "2014-04-26 17:24:37.1 +0000 UTC", out: "2014-04-26 17:24:37.1 +0000 UTC"}, + {in: "2015-02-08 03:02:00 +0200 CEST m=+0.000000001", out: "2015-02-08 01:02:00 +0000 UTC", loc: "Europe/Berlin"}, + {in: "2015-02-08 03:02:00 +0300 MSK m=+0.000000001", out: "2015-02-08 00:02:00 +0000 UTC"}, + {in: "2015-02-08 03:02:00.001 +0300 MSK m=+0.000000001", out: "2015-02-08 00:02:00.001 +0000 UTC"}, // yyyy-mm-dd hh:mm:ss TZ {in: "2012-08-03 18:31:59 UTC", out: "2012-08-03 18:31:59 +0000 UTC"}, {in: "2014-12-16 06:20:00 GMT", out: "2014-12-16 06:20:00 +0000 UTC"},