Skip to content

Commit 36fa6fb

Browse files
author
Aaron Raddon
committed
fix TZ-location override for format fixes #113
1 parent 0360d12 commit 36fa6fb

File tree

2 files changed

+38
-11
lines changed

2 files changed

+38
-11
lines changed

parseany.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1292,7 +1292,6 @@ iterRunes:
12921292
// 17:57:51 MST 2009
12931293
p.tzi = i
12941294
p.stateTime = timeWsAlpha
1295-
//break iterTimeRunes
12961295
} else if unicode.IsDigit(r) {
12971296
// 00:12:00 2008
12981297
p.stateTime = timeWsYear
@@ -1322,6 +1321,7 @@ iterRunes:
13221321
p.offseti = i
13231322
case ' ':
13241323
// 17:57:51 MST 2009
1324+
// 17:57:51 MST
13251325
p.tzlen = i - p.tzi
13261326
if p.tzlen == 4 {
13271327
p.set(p.tzi, " MST")
@@ -1441,6 +1441,7 @@ iterRunes:
14411441
p.setYear()
14421442
}
14431443
case unicode.IsLetter(r):
1444+
// 15:04:05 -0700 MST
14441445
if p.tzi == 0 {
14451446
p.tzi = i
14461447
}
@@ -1626,6 +1627,17 @@ iterRunes:
16261627
}
16271628

16281629
switch p.stateTime {
1630+
case timeWsAlpha:
1631+
switch len(p.datestr) - p.tzi {
1632+
case 3:
1633+
// 13:31:51.999 +01:00 CET
1634+
p.set(p.tzi, "MST")
1635+
case 4:
1636+
p.set(p.tzi, "MST")
1637+
p.extra = len(p.datestr) - 1
1638+
p.trimExtra()
1639+
}
1640+
16291641
case timeWsAlphaWs:
16301642
p.yearlen = i - p.yeari
16311643
p.setYear()
@@ -1662,6 +1674,17 @@ iterRunes:
16621674
case timeWsOffsetWs:
16631675
// 17:57:51 -0700 2009
16641676
// 00:12:00 +0000 UTC
1677+
if p.tzi > 0 {
1678+
switch len(p.datestr) - p.tzi {
1679+
case 3:
1680+
// 13:31:51.999 +01:00 CET
1681+
p.set(p.tzi, "MST")
1682+
case 4:
1683+
// 13:31:51.999 +01:00 CEST
1684+
p.set(p.tzi, "MST ")
1685+
}
1686+
1687+
}
16651688
case timeWsOffsetColon:
16661689
// 17:57:51 -07:00
16671690
p.set(p.offseti, "-07:00")
@@ -2115,7 +2138,7 @@ func (p *parser) parse() (time.Time, error) {
21152138
}
21162139

21172140
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)
2141+
// gou.Debugf("parse layout=%q input=%q \ntx, err := time.Parse(%q, %q)", string(p.format), p.datestr, string(p.format), p.datestr)
21192142
return time.Parse(string(p.format), p.datestr)
21202143
}
21212144
//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)

parseany_test.go

Lines changed: 13 additions & 9 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("2019-05-29T08:41-04")
15-
assert.Equal(t, "2019-05-29 12:41:00 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))
14+
ts = MustParse("Thu, 17 Dec 2020 15:39:13 GMT")
15+
assert.Equal(t, "2020-12-17 15:39:13 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))
1616
}
1717

1818
type dateTest struct {
@@ -107,9 +107,10 @@ var testInputs = []dateTest{
107107
{in: "June 2nd 2012", out: "2012-06-02 00:00:00 +0000 UTC"},
108108
{in: "June 22nd, 2012", out: "2012-06-22 00:00:00 +0000 UTC"},
109109
{in: "June 22nd 2012", out: "2012-06-22 00:00:00 +0000 UTC"},
110-
// ?
110+
// RFC1123 = "Mon, 02 Jan 2006 15:04:05 MST"
111111
{in: "Fri, 03 Jul 2015 08:08:08 MST", out: "2015-07-03 08:08:08 +0000 UTC"},
112-
{in: "Fri, 03 Jul 2015 08:08:08 PST", out: "2015-07-03 15:08:08 +0000 UTC", loc: "America/Los_Angeles"},
112+
//{in: "Fri, 03 Jul 2015 08:08:08 CET", out: "2015-07-03 08:08:08 +0000 UTC"},
113+
{in: "Fri, 03 Jul 2015 08:08:08 PST", out: "2015-07-03 16:08:08 +0000 UTC", loc: "America/Los_Angeles"},
113114
{in: "Fri, 03 Jul 2015 08:08:08 PST", out: "2015-07-03 08:08:08 +0000 UTC"},
114115
{in: "Fri, 3 Jul 2015 08:08:08 MST", out: "2015-07-03 08:08:08 +0000 UTC"},
115116
{in: "Fri, 03 Jul 2015 8:08:08 MST", out: "2015-07-03 08:08:08 +0000 UTC"},
@@ -126,7 +127,7 @@ var testInputs = []dateTest{
126127
{in: "Tue, 11 Jul 2017 04:08:03 +0200 (CEST)", out: "2017-07-11 02:08:03 +0000 UTC", loc: "Europe/Berlin"},
127128
// day, dd-Mon-yy hh:mm:zz TZ
128129
{in: "Fri, 03-Jul-15 08:08:08 MST", out: "2015-07-03 08:08:08 +0000 UTC"},
129-
{in: "Fri, 03-Jul-15 08:08:08 PST", out: "2015-07-03 15:08:08 +0000 UTC", loc: "America/Los_Angeles"},
130+
{in: "Fri, 03-Jul-15 08:08:08 PST", out: "2015-07-03 16:08:08 +0000 UTC", loc: "America/Los_Angeles"},
130131
{in: "Fri, 03-Jul 2015 08:08:08 PST", out: "2015-07-03 08:08:08 +0000 UTC"},
131132
{in: "Fri, 3-Jul-15 08:08:08 MST", out: "2015-07-03 08:08:08 +0000 UTC"},
132133
{in: "Fri, 03-Jul-15 8:08:08 MST", out: "2015-07-03 08:08:08 +0000 UTC"},
@@ -326,7 +327,7 @@ var testInputs = []dateTest{
326327
{in: "2014-04-26 17:24:37.1 UTC", out: "2014-04-26 17:24:37.1 +0000 UTC"},
327328
// This one is pretty special, it is TIMEZONE based but starts with P to emulate collions with PM
328329
{in: "2014-04-26 05:24:37 PST", out: "2014-04-26 05:24:37 +0000 UTC"},
329-
{in: "2014-04-26 05:24:37 PST", out: "2014-04-26 12:24:37 +0000 UTC", loc: "America/Los_Angeles"},
330+
{in: "2014-04-26 05:24:37 PST", out: "2014-04-26 13:24:37 +0000 UTC", loc: "America/Los_Angeles"},
330331
// yyyy-mm-dd hh:mm:ss+00:00
331332
{in: "2012-08-03 18:31:59+00:00", out: "2012-08-03 18:31:59 +0000 UTC"},
332333
{in: "2017-07-19 03:21:51+00:00", out: "2017-07-19 03:21:51 +0000 UTC"},
@@ -520,6 +521,8 @@ func TestParseErrors(t *testing.T) {
520521
}
521522

522523
func TestParseLayout(t *testing.T) {
524+
525+
time.Local = time.UTC
523526
// These tests are verifying that the layout returned by ParseFormat
524527
// are correct. Above tests correct parsing, this tests correct
525528
// re-usable formatting string
@@ -546,10 +549,11 @@ func TestParseLayout(t *testing.T) {
546549
// yyyy-mm-dd hh:mm:ss +00:00
547550
{in: "2012-08-03 18:31:59 +00:00", out: "2006-01-02 15:04:05 -07:00"},
548551
// yyyy-mm-dd hh:mm:ss +0000 TZ
549-
// Golang Native Format
550-
{in: "2012-08-03 18:31:59 +0000 UTC", out: "2006-01-02 15:04:05 -0700 UTC"},
552+
// Golang Native Format = "2006-01-02 15:04:05.999999999 -0700 MST"
553+
{in: "2012-08-03 18:31:59 +0000 UTC", out: "2006-01-02 15:04:05 -0700 MST"},
551554
// yyyy-mm-dd hh:mm:ss TZ
552-
{in: "2012-08-03 18:31:59 UTC", out: "2006-01-02 15:04:05 UTC"},
555+
{in: "2012-08-03 18:31:59 UTC", out: "2006-01-02 15:04:05 MST"},
556+
{in: "2012-08-03 18:31:59 CEST", out: "2006-01-02 15:04:05 MST"},
553557
// yyyy-mm-ddThh:mm:ss-07:00
554558
{in: "2009-08-12T22:15:09-07:00", out: "2006-01-02T15:04:05-07:00"},
555559
// yyyy-mm-ddThh:mm:ss-0700

0 commit comments

Comments
 (0)