Skip to content

Commit 2b3f700

Browse files
Handle format "date time (MST)"
Was unable to handle standalone timezone in parentheses before. Also update tests to indicate expected timezone name for all tests that are parsed in a specific location. With updated logic/fixes, add tests to verify: * Fix araddon#71 * Fix araddon#72
1 parent 8f0059d commit 2b3f700

2 files changed

Lines changed: 59 additions & 25 deletions

File tree

parseany.go

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -106,18 +106,19 @@ const (
106106
timeStart
107107
timeWs
108108
timeWsAlpha
109+
timeWsAlphaRParen
109110
timeWsAlphaWs
110-
timeWsAlphaZoneOffset // 5
111+
timeWsAlphaZoneOffset // 6
111112
timeWsAlphaZoneOffsetWs
112113
timeWsAlphaZoneOffsetWsYear
113114
timeWsAlphaZoneOffsetWsExtra
114115
timeWsAMPMMaybe
115-
timeWsAMPM // 10
116+
timeWsAMPM // 11
116117
timeWsOffset
117-
timeWsOffsetWs // 12
118+
timeWsOffsetWs // 13
118119
timeWsOffsetColonAlpha
119120
timeWsOffsetColon
120-
timeWsYear // 15
121+
timeWsYear // 16
121122
timeOffset
122123
timeOffsetColon
123124
timeOffsetColonAlpha
@@ -1615,6 +1616,7 @@ iterRunes:
16151616
case timeWsAlpha:
16161617
// 06:20:00 UTC
16171618
// 06:20:00 UTC-05
1619+
// 06:20:00 (EST)
16181620
// timeWsAlphaWs
16191621
// 17:57:51 MST 2009
16201622
// timeWsAlphaZoneOffset
@@ -1638,17 +1640,28 @@ iterRunes:
16381640
}
16391641
p.stateTime = timeWsAlphaZoneOffset
16401642
p.offseti = i
1641-
case ' ':
1643+
case ' ', ')':
16421644
// 17:57:51 MST 2009
16431645
// 17:57:51 MST
1646+
// 06:20:00 (EST)
16441647
p.tzlen = i - p.tzi
16451648
if p.tzlen == 4 {
16461649
p.set(p.tzi, " MST")
16471650
} else if p.tzlen == 3 {
16481651
p.set(p.tzi, "MST")
16491652
}
1650-
p.stateTime = timeWsAlphaWs
1651-
p.yeari = i + 1
1653+
if r == ' ' {
1654+
p.stateTime = timeWsAlphaWs
1655+
p.yeari = i + 1
1656+
} else {
1657+
// 06:20:00 (EST)
1658+
// This must be the end of the datetime or the format is unknown
1659+
if i+1 == len(p.datestr) {
1660+
p.stateTime = timeWsAlphaRParen
1661+
} else {
1662+
return p, unknownErr(datestr)
1663+
}
1664+
}
16521665
}
16531666
case timeWsAlphaWs:
16541667
// 17:57:51 MST 2009
@@ -1923,6 +1936,9 @@ iterRunes:
19231936
return p, fmt.Errorf("timezone not recognized %q near %q (must be 3 or 4 characters)", datestr, string(p.datestr[p.tzi:]))
19241937
}
19251938

1939+
case timeWsAlphaRParen:
1940+
// continue
1941+
19261942
case timeWsAlphaWs:
19271943
p.yearlen = i - p.yeari
19281944
if !p.setYear() {

parseany_test.go

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ var testInputs = []dateTest{
6060
{in: "Thu May 8 17:57:51 PST 2009", out: "2009-05-08 17:57:51 +0000 UTC", zname: "PST"},
6161
{in: "Thu May 08 17:57:51 PST 2009", out: "2009-05-08 17:57:51 +0000 UTC", zname: "PST"},
6262
{in: "Thu May 08 17:57:51 CEST 2009", out: "2009-05-08 17:57:51 +0000 UTC", zname: "CEST"},
63-
{in: "Thu May 08 17:57:51 CEST 2009", out: "2009-05-08 15:57:51 +0000 UTC", loc: "Europe/Berlin"},
63+
{in: "Thu May 08 17:57:51 CEST 2009", out: "2009-05-08 15:57:51 +0000 UTC", loc: "Europe/Berlin", zname: "CEST"},
6464
{in: "Thu May 08 05:05:07 PST 2009", out: "2009-05-08 05:05:07 +0000 UTC", zname: "PST"},
6565
{in: "Thu May 08 5:5:7 PST 2009", out: "2009-05-08 05:05:07 +0000 UTC", zname: "PST"},
6666
// Day Month dd time
@@ -145,7 +145,7 @@ var testInputs = []dateTest{
145145
// RFC1123 = "Mon, 02 Jan 2006 15:04:05 MST"
146146
{in: "Fri, 03 Jul 2015 08:08:08 MST", out: "2015-07-03 08:08:08 +0000 UTC", zname: "MST"},
147147
{in: "Fri, 03 Jul 2015 08:08:08 CET", out: "2015-07-03 08:08:08 +0000 UTC", zname: "CET"},
148-
{in: "Fri, 03 Jul 2015 08:08:08 PST", out: "2015-07-03 16:08:08 +0000 UTC", loc: "America/Los_Angeles"},
148+
{in: "Fri, 03 Jul 2015 08:08:08 PST", out: "2015-07-03 16:08:08 +0000 UTC", loc: "America/Los_Angeles", zname: "PDT"},
149149
{in: "Fri, 03 Jul 2015 08:08:08 PST", out: "2015-07-03 08:08:08 +0000 UTC", zname: "PST"},
150150
{in: "Fri, 03 Jul 2015 08:08:08 CEST", out: "2015-07-03 08:08:08 +0000 UTC", zname: "CEST"},
151151
{in: "Fri, 3 Jul 2015 08:08:08 MST", out: "2015-07-03 08:08:08 +0000 UTC", zname: "MST"},
@@ -163,11 +163,11 @@ var testInputs = []dateTest{
163163
//
164164
{in: "Tue, 11 Jul 2017 04:08:03 +0200 (CEST)", out: "2017-07-11 02:08:03 +0000 UTC"},
165165
{in: "Tue, 5 Jul 2017 04:08:03 -0700 (MST)", out: "2017-07-05 11:08:03 +0000 UTC"},
166-
{in: "Tue, 11 Jul 2017 04:08:03 +0200 (CEST)", out: "2017-07-11 02:08:03 +0000 UTC", loc: "Europe/Berlin"},
166+
{in: "Tue, 11 Jul 2017 04:08:03 +0200 (CEST)", out: "2017-07-11 02:08:03 +0000 UTC", loc: "Europe/Berlin", zname: "CEST"},
167167
// day, dd-Mon-yy hh:mm:zz TZ
168168
{in: "Fri, 03-Jul-15 08:08:08 MST", out: "2015-07-03 08:08:08 +0000 UTC", zname: "MST"},
169169
{in: "Fri, 03-Jul-15 08:08:08 CEST", out: "2015-07-03 08:08:08 +0000 UTC", zname: "CEST"},
170-
{in: "Fri, 03-Jul-15 08:08:08 PST", out: "2015-07-03 16:08:08 +0000 UTC", loc: "America/Los_Angeles"},
170+
{in: "Fri, 03-Jul-15 08:08:08 PST", out: "2015-07-03 16:08:08 +0000 UTC", loc: "America/Los_Angeles", zname: "PDT"},
171171
{in: "Fri, 03-Jul 2015 08:08:08 PST", out: "2015-07-03 08:08:08 +0000 UTC", zname: "PST"},
172172
{in: "Fri, 03-Jul 2015 08:08:08 CEST", out: "2015-07-03 08:08:08 +0000 UTC", zname: "CEST"},
173173
{in: "Fri, 3-Jul-15 08:08:08 MST", out: "2015-07-03 08:08:08 +0000 UTC", zname: "MST"},
@@ -182,7 +182,7 @@ var testInputs = []dateTest{
182182
{in: "Wednesday, 07-May-09 08:00:43 MST", out: "2009-05-07 08:00:43 +0000 UTC", zname: "MST"},
183183
{in: "Wednesday, 07-May-09 08:00:43 CEST", out: "2009-05-07 08:00:43 +0000 UTC", zname: "CEST"},
184184
{in: "Wednesday, 28-Feb-18 09:01:00 MST", out: "2018-02-28 09:01:00 +0000 UTC", zname: "MST"},
185-
{in: "Wednesday, 28-Feb-18 09:01:00 MST", out: "2018-02-28 16:01:00 +0000 UTC", loc: "America/Denver"},
185+
{in: "Wednesday, 28-Feb-18 09:01:00 MST", out: "2018-02-28 16:01:00 +0000 UTC", loc: "America/Denver", zname: "MST"},
186186
{in: "Wednesday, 28-Feb-18 09:01:00 CEST", out: "2018-02-28 09:01:00 +0000 UTC", zname: "CEST"},
187187
// with offset then with variations on non-zero filled stuff
188188
{in: "Monday, 02 Jan 2006 15:04:05 +0100", out: "2006-01-02 14:04:05 +0000 UTC"},
@@ -421,6 +421,11 @@ var testInputs = []dateTest{
421421
{in: "2013 December 02 11:37:55", out: "2013-12-02 11:37:55 +0000 UTC"},
422422
// https://github.com/araddon/dateparse/issues/143
423423
{in: "20140722105203.364", out: "2014-07-22 10:52:03.364 +0000 UTC"},
424+
// https://github.com/araddon/dateparse/issues/71 and https://github.com/araddon/dateparse/issues/72
425+
{in: "2017-12-31T16:00:00Z", out: "2017-12-31 16:00:00 +0000 UTC", loc: "America/Denver", zname: "UTC"},
426+
{in: "Jul 9, 2012 at 5:02am (EST)", out: "2012-07-09 05:02:00 +0000 UTC", zname: "EST"},
427+
{in: "Jul 9, 2012 at 5:02am (EST)", out: "2012-07-09 05:02:00 +0000 UTC", loc: "US/Pacific", zname: "EST"},
428+
{in: "Jul 9, 2012 at 5:02am (EST)", out: "2012-07-09 10:02:00 +0000 UTC", loc: "America/New_York", zname: "EDT"},
424429
// yyyy-mm-dd hh:mm:ss,000
425430
{in: "2014-05-11 08:20:13,787", out: "2014-05-11 08:20:13.787 +0000 UTC"},
426431
// yyyy-mm-dd hh:mm:ss +0000
@@ -458,10 +463,10 @@ var testInputs = []dateTest{
458463
// yyyy-mm-dd hh:mm:ss +0000 TZ
459464
// Golang Native Format
460465
{in: "2012-08-03 18:31:59 +0000 UTC", out: "2012-08-03 18:31:59 +0000 UTC", zname: "UTC"},
461-
{in: "2012-08-03 13:31:59 -0600 MST", out: "2012-08-03 19:31:59 +0000 UTC", loc: "America/Denver"},
466+
{in: "2012-08-03 13:31:59 -0600 MST", out: "2012-08-03 19:31:59 +0000 UTC", loc: "America/Denver", zname: "MST"},
462467
{in: "2015-02-18 00:12:00 +0000 UTC", out: "2015-02-18 00:12:00 +0000 UTC", zname: "UTC"},
463468
{in: "2015-02-18 00:12:00 +0000 GMT", out: "2015-02-18 00:12:00 +0000 UTC", zname: "GMT"},
464-
{in: "2015-02-08 03:02:00 +0200 CEST", out: "2015-02-08 01:02:00 +0000 UTC", loc: "Europe/Berlin"},
469+
{in: "2015-02-08 03:02:00 +0200 CEST", out: "2015-02-08 01:02:00 +0000 UTC", loc: "Europe/Berlin", zname: "CEST"},
465470
{in: "2015-02-08 03:02:00 +0300 MSK", out: "2015-02-08 00:02:00 +0000 UTC", zname: "MSK"},
466471
{in: "2015-2-08 03:02:00 +0300 MSK", out: "2015-02-08 00:02:00 +0000 UTC", zname: "MSK"},
467472
{in: "2015-02-8 03:02:00 +0300 MSK", out: "2015-02-08 00:02:00 +0000 UTC", zname: "MSK"},
@@ -473,14 +478,16 @@ var testInputs = []dateTest{
473478
{in: "2014-04-26 17:24:37.123456 +0000 UTC", out: "2014-04-26 17:24:37.123456 +0000 UTC", zname: "UTC"},
474479
{in: "2014-04-26 17:24:37.12 +0000 UTC", out: "2014-04-26 17:24:37.12 +0000 UTC", zname: "UTC"},
475480
{in: "2014-04-26 17:24:37.1 +0000 UTC", out: "2014-04-26 17:24:37.1 +0000 UTC", zname: "UTC"},
476-
{in: "2015-02-08 03:02:00 +0200 CEST m=+0.000000001", out: "2015-02-08 01:02:00 +0000 UTC", loc: "Europe/Berlin"},
481+
{in: "2015-02-08 03:02:00 +0200 CEST m=+0.000000001", out: "2015-02-08 01:02:00 +0000 UTC", loc: "Europe/Berlin", zname: "CEST"},
477482
{in: "2015-02-08 03:02:00 +0300 MSK m=+0.000000001", out: "2015-02-08 00:02:00 +0000 UTC", zname: "MSK"},
478483
{in: "2015-02-08 03:02:00.001 +0300 MSK m=+0.000000001", out: "2015-02-08 00:02:00.001 +0000 UTC", zname: "MSK"},
479484
// yyyy-mm-dd hh:mm:ss TZ
480485
{in: "2012-08-03 18:31:59 UTC", out: "2012-08-03 18:31:59 +0000 UTC", zname: "UTC"},
481486
{in: "2012-08-03 18:31:59 CEST", out: "2012-08-03 18:31:59 +0000 UTC", zname: "CEST"},
482487
{in: "2014-12-16 06:20:00 GMT", out: "2014-12-16 06:20:00 +0000 UTC", zname: "GMT"},
483-
{in: "2012-08-03 13:31:59 MST", out: "2012-08-03 20:31:59 +0000 UTC", loc: "America/Denver"},
488+
{in: "2012-08-03 13:31:58 MST", out: "2012-08-03 13:31:58 +0000 UTC", zname: "MST"},
489+
{in: "2012-08-03 13:31:59 MST", out: "2012-08-03 20:31:59 +0000 UTC", loc: "America/Denver", zname: "MDT"},
490+
{in: "2012-01-03 13:31:59 MST", out: "2012-01-03 20:31:59 +0000 UTC", loc: "America/Denver", zname: "MST"},
484491
{in: "2012-08-03 18:31:59.257000000 UTC", out: "2012-08-03 18:31:59.257 +0000 UTC", zname: "UTC"},
485492
{in: "2012-08-03 8:1:59.257000000 UTC", out: "2012-08-03 08:01:59.257 +0000 UTC", zname: "UTC"},
486493
{in: "2012-8-03 18:31:59.257000000 UTC", out: "2012-08-03 18:31:59.257 +0000 UTC", zname: "UTC"},
@@ -493,9 +500,15 @@ var testInputs = []dateTest{
493500
{in: "2014-04-26 17:24:37.12 CEST", out: "2014-04-26 17:24:37.12 +0000 UTC", zname: "CEST"},
494501
{in: "2014-04-26 17:24:37.1 UTC", out: "2014-04-26 17:24:37.1 +0000 UTC", zname: "UTC"},
495502
{in: "2014-04-26 17:24:37.1 CEST", out: "2014-04-26 17:24:37.1 +0000 UTC", zname: "CEST"},
503+
// Test the capturing of arbitrary time zone names even if we use a different specific location (offset will be zero, but name will be filled in)
504+
{in: "2012-08-03 19:32:59 UTC", out: "2012-08-03 19:32:59 +0000 UTC", loc: "Europe/Berlin", zname: "UTC"},
505+
{in: "2012-08-03 19:32:59 CEST", out: "2012-08-03 19:32:59 +0000 UTC", loc: "America/Denver", zname: "CEST"},
506+
{in: "2014-12-16 07:22:00 GMT", out: "2014-12-16 07:22:00 +0000 UTC", loc: "America/Los_Angeles", zname: "GMT"},
507+
{in: "2012-08-03 14:32:59 MST", out: "2012-08-03 14:32:59 +0000 UTC", loc: "America/Los_Angeles", zname: "MST"},
496508
// This one is pretty special, it is TIMEZONE based but starts with P to emulate collions with PM
497509
{in: "2014-04-26 05:24:37 PST", out: "2014-04-26 05:24:37 +0000 UTC", zname: "PST"},
498-
{in: "2014-04-26 05:24:37 PST", out: "2014-04-26 13:24:37 +0000 UTC", loc: "America/Los_Angeles"},
510+
{in: "2014-04-26 05:24:38 PST", out: "2014-04-26 13:24:38 +0000 UTC", loc: "America/Los_Angeles", zname: "PDT"},
511+
{in: "2014-01-26 05:24:39 PST", out: "2014-01-26 13:24:39 +0000 UTC", loc: "America/Los_Angeles", zname: "PST"},
499512
// yyyy-mm-dd hh:mm:ss+00:00
500513
{in: "2012-08-03 18:31:59+00:00", out: "2012-08-03 18:31:59 +0000 UTC"},
501514
{in: "2017-07-19 03:21:51+00:00", out: "2017-07-19 03:21:51 +0000 UTC"},
@@ -504,15 +517,16 @@ var testInputs = []dateTest{
504517
// dd:mm:yyyy hh:mm:ss+00:00
505518
{in: "08:03:2012 18:31:59+00:00", out: "2012-08-03 18:31:59 +0000 UTC"},
506519
// yyyy-mm-dd hh:mm:ss.000+00:00 PST
507-
{in: "2012-08-03 18:31:59.000+00:00 PST", out: "2012-08-03 18:31:59 +0000 UTC", loc: "America/Los_Angeles"},
508-
{in: "2012-08-03 18:31:59.000+00:00 CEST", out: "2012-08-03 18:31:59 +0000 UTC", loc: "Europe/Berlin"},
520+
{in: "2012-08-03 18:31:59.000+00:00 PST", out: "2012-08-03 18:31:59 +0000 UTC", loc: "America/Los_Angeles", zname: "PST"},
521+
{in: "2012-08-03 18:31:59.000+00:00 CEST", out: "2012-08-03 18:31:59 +0000 UTC", loc: "Europe/Berlin", zname: "CEST"},
509522
// yyyy-mm-dd hh:mm:ss +00:00 TZ
510523
{in: "2012-08-03 18:31:59 +00:00 UTC", out: "2012-08-03 18:31:59 +0000 UTC", zname: "UTC"},
511-
{in: "2012-08-03 13:31:51 -07:00 MST", out: "2012-08-03 20:31:51 +0000 UTC", loc: "America/Denver"},
512-
{in: "2012-08-03 13:31:51 +02:00 CEST", out: "2012-08-03 11:31:51 +0000 UTC", loc: "Europe/Berlin"},
524+
{in: "2012-08-03 13:31:51 -07:00 MST", out: "2012-08-03 20:31:51 +0000 UTC", loc: "America/Denver", zname: "MST"},
525+
{in: "2012-08-03 13:31:51 +02:00 CEST", out: "2012-08-03 11:31:51 +0000 UTC", loc: "Europe/Berlin", zname: "CEST"},
513526
{in: "2012-08-03 18:31:59.257000000 +00:00 UTC", out: "2012-08-03 18:31:59.257 +0000 UTC", zname: "UTC"},
514-
{in: "2012-08-03 13:31:51.123 -08:00 PST", out: "2012-08-03 21:31:51.123 +0000 UTC", loc: "America/Los_Angeles"},
515-
{in: "2012-08-03 13:31:51.123 +02:00 CEST", out: "2012-08-03 11:31:51.123 +0000 UTC", loc: "Europe/Berlin"},
527+
{in: "2012-08-03 13:31:51.123 -08:00 PST", out: "2012-08-03 21:31:51.123 +0000 UTC", loc: "America/Los_Angeles", zname: "PST"},
528+
{in: "2012-08-03 13:31:51.123 +02:00 CEST", out: "2012-08-03 11:31:51.123 +0000 UTC", loc: "Europe/Berlin", zname: "CEST"},
529+
{in: "2012-08-03 13:31:51.123 +02:00 CEST", out: "2012-08-03 11:31:51.123 +0000 UTC", loc: "America/Los_Angeles", zname: "CEST"},
516530
{in: "2012-08-03 8:1:59.257000000 +00:00 UTC", out: "2012-08-03 08:01:59.257 +0000 UTC", zname: "UTC"},
517531
{in: "2012-08-03 8:1:59.257000000 +00:00 CEST", out: "2012-08-03 08:01:59.257 +0000 UTC", zname: "CEST"},
518532
{in: "2012-8-03 18:31:59.257000000 +00:00 UTC", out: "2012-08-03 18:31:59.257 +0000 UTC", zname: "UTC"},
@@ -613,8 +627,8 @@ var testInputs = []dateTest{
613627
{in: "190910 11:51:49", out: "2019-09-10 11:51:49 +0000 UTC"},
614628

615629
// all digits: unix secs, ms etc
616-
{in: "1332151919", out: "2012-03-19 10:11:59 +0000 UTC"},
617-
{in: "1332151919", out: "2012-03-19 10:11:59 +0000 UTC", loc: "America/Denver"},
630+
{in: "1332151919", out: "2012-03-19 10:11:59 +0000 UTC", zname: "UTC"},
631+
{in: "1332151919", out: "2012-03-19 10:11:59 +0000 UTC", loc: "America/Denver", zname: "MDT"},
618632
{in: "1384216367111", out: "2013-11-12 00:32:47.111 +0000 UTC"},
619633
{in: "1384216367111222", out: "2013-11-12 00:32:47.111222 +0000 UTC"},
620634
{in: "1384216367111222333", out: "2013-11-12 00:32:47.111222333 +0000 UTC"},
@@ -1040,3 +1054,7 @@ func TestRetryAmbiguousDateWithSwap(t *testing.T) {
10401054
assert.Equal(t, nil, err)
10411055
assert.Equal(t, "2014-02-13 04:08:09 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))
10421056
}
1057+
1058+
func TestDebug(t *testing.T) {
1059+
MustParse("Jul 9, 2012 at 5:02am (EST)")
1060+
}

0 commit comments

Comments
 (0)