Skip to content

Commit 0360d12

Browse files
author
Aaron Raddon
committed
Support len 2 TZ offsets: 2019-05-29T08:41-04 fixes #111
1 parent bbfb8e0 commit 0360d12

File tree

4 files changed

+27
-7
lines changed

4 files changed

+27
-7
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ var examples = []string{
130130
"2009-08-12T22:15:09.988",
131131
"2009-08-12T22:15:09Z",
132132
"2017-07-19T03:21:51:897+0100",
133+
"2019-05-29T08:41-04", // no seconds, 2 digit TZ offset
133134
// yyyy-mm-dd hh:mm:ss
134135
"2014-04-26 17:24:37.3186369",
135136
"2012-08-03 18:31:59.257000000",
@@ -278,6 +279,7 @@ func main() {
278279
| 2009-08-12T22:15:09.988 | 2009-08-12 22:15:09.988 +0000 UTC |
279280
| 2009-08-12T22:15:09Z | 2009-08-12 22:15:09 +0000 UTC |
280281
| 2017-07-19T03:21:51:897+0100 | 2017-07-19 03:21:51.897 +0100 +0100 |
282+
| 2019-05-29T08:41-04 | 2019-05-29 08:41:00 -0400 -0400 |
281283
| 2014-04-26 17:24:37.3186369 | 2014-04-26 17:24:37.3186369 +0000 UTC |
282284
| 2012-08-03 18:31:59.257000000 | 2012-08-03 18:31:59.257 +0000 UTC |
283285
| 2014-04-26 17:24:37.123 | 2014-04-26 17:24:37.123 +0000 UTC |

example/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ var examples = []string{
8787
"2009-08-12T22:15:09.988",
8888
"2009-08-12T22:15:09Z",
8989
"2017-07-19T03:21:51:897+0100",
90+
"2019-05-29T08:41-04", // no seconds, 2 digit TZ offset
9091
// yyyy-mm-dd hh:mm:ss
9192
"2014-04-26 17:24:37.3186369",
9293
"2012-08-03 18:31:59.257000000",
@@ -235,6 +236,7 @@ func main() {
235236
| 2009-08-12T22:15:09.988 | 2009-08-12 22:15:09.988 +0000 UTC |
236237
| 2009-08-12T22:15:09Z | 2009-08-12 22:15:09 +0000 UTC |
237238
| 2017-07-19T03:21:51:897+0100 | 2017-07-19 03:21:51.897 +0100 +0100 |
239+
| 2019-05-29T08:41-04 | 2019-05-29 08:41:00 -0400 -0400 |
238240
| 2014-04-26 17:24:37.3186369 | 2014-04-26 17:24:37.3186369 +0000 UTC |
239241
| 2012-08-03 18:31:59.257000000 | 2012-08-03 18:31:59.257 +0000 UTC |
240242
| 2014-04-26 17:24:37.123 | 2014-04-26 17:24:37.123 +0000 UTC |

parseany.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1645,8 +1645,18 @@ iterRunes:
16451645
case timePeriod:
16461646
p.mslen = i - p.msi
16471647
case timeOffset:
1648-
// 19:55:00+0100
1649-
p.set(p.offseti, "-0700")
1648+
1649+
switch len(p.datestr) - p.offseti {
1650+
case 0, 1, 2, 4:
1651+
return p, fmt.Errorf("TZ offset not recognized %q near %q (must be 2 or 4 digits optional colon)", datestr, string(datestr[p.offseti:]))
1652+
case 3:
1653+
// 19:55:00+01
1654+
p.set(p.offseti, "-07")
1655+
case 5:
1656+
// 19:55:00+0100
1657+
p.set(p.offseti, "-0700")
1658+
}
1659+
16501660
case timeWsOffset:
16511661
p.set(p.offseti, "-0700")
16521662
case timeWsOffsetWs:
@@ -2104,10 +2114,11 @@ func (p *parser) parse() (time.Time, error) {
21042114
p.datestr = p.datestr[p.skip:]
21052115
}
21062116

2107-
// gou.Debugf("parse %q AS %q", p.datestr, string(p.format))
21082117
if p.loc == nil {
2118+
//gou.Debugf("parse layout=%q input=%q \ntx, err := time.Parse(%q, %q)", string(p.format), p.datestr, string(p.format), p.datestr)
21092119
return time.Parse(string(p.format), p.datestr)
21102120
}
2121+
//gou.Debugf("parse layout=%q input=%q \ntx, err := time.ParseInLocation(%q, %q, %v)", string(p.format), p.datestr, string(p.format), p.datestr, p.loc)
21112122
return time.ParseInLocation(string(p.format), p.datestr, p.loc)
21122123
}
21132124
func isDay(alpha string) bool {

parseany_test.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import (
1111
func TestOne(t *testing.T) {
1212
time.Local = time.UTC
1313
var ts time.Time
14-
ts = MustParse("2020-08-17T17:00:00:987+0100")
15-
assert.Equal(t, "2020-08-17 16:00:00.987 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))
14+
ts = MustParse("2019-05-29T08:41-04")
15+
assert.Equal(t, "2019-05-29 12:41:00 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))
1616
}
1717

1818
type dateTest struct {
@@ -364,6 +364,8 @@ var testInputs = []dateTest{
364364
{in: "2009-08-12T22:15:09.123-07:00", out: "2009-08-13 05:15:09.123 +0000 UTC"},
365365
{in: "2016-06-21T19:55:00+01:00", out: "2016-06-21 18:55:00 +0000 UTC"},
366366
{in: "2016-06-21T19:55:00.799+01:00", out: "2016-06-21 18:55:00.799 +0000 UTC"},
367+
// yyyy-mm-ddThh:mm:ss-07 TZ truncated to 2 digits instead of 4
368+
{in: "2019-05-29T08:41-04", out: "2019-05-29 12:41:00 +0000 UTC"},
367369
// yyyy-mm-ddThh:mm:ss-0700
368370
{in: "2009-08-12T22:15:09-0700", out: "2009-08-13 05:15:09 +0000 UTC"},
369371
{in: "2009-08-12T22:15:09-0300", out: "2009-08-13 01:15:09 +0000 UTC"},
@@ -376,8 +378,9 @@ var testInputs = []dateTest{
376378
{in: "2016-06-21T19:55:00.799+0100", out: "2016-06-21 18:55:00.799 +0000 UTC"},
377379
{in: "2016-06-21T19:55+0100", out: "2016-06-21 18:55:00 +0000 UTC"},
378380
{in: "2016-06-21T19:55+0130", out: "2016-06-21 18:25:00 +0000 UTC"},
379-
// yyyy-mm-ddThh:mm:ss:000+0000
380-
{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
381+
// yyyy-mm-ddThh:mm:ss:000+0000 - weird format with additional colon in front of milliseconds
382+
{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
383+
381384
// yyyy-mm-ddThh:mm:ssZ
382385
{in: "2009-08-12T22:15Z", out: "2009-08-12 22:15:00 +0000 UTC"},
383386
{in: "2009-08-12T22:15:09Z", out: "2009-08-12 22:15:09 +0000 UTC"},
@@ -526,6 +529,8 @@ func TestParseLayout(t *testing.T) {
526529
{in: `{"hello"}`, err: true},
527530
{in: "2009-15-12T22:15Z", err: true},
528531
{in: "5,000-9,999", err: true},
532+
// This 3 digit TZ offset (should be 2 or 4? is 3 a thing?)
533+
{in: "2019-05-29T08:41-047", err: true},
529534
//
530535
{in: "06/May/2008 15:04:05 -0700", out: "02/Jan/2006 15:04:05 -0700"},
531536
{in: "06/May/2008:15:04:05 -0700", out: "02/Jan/2006:15:04:05 -0700"},

0 commit comments

Comments
 (0)