Fix timezone detection when time has fractional component (#544)

This commit is contained in:
Thomas Pelletier
2021-05-21 09:37:43 -04:00
committed by GitHub
parent 238a6fef7d
commit c2d1fd86e5
4 changed files with 46 additions and 3 deletions
+17
View File
@@ -145,6 +145,23 @@ func BenchmarkReferenceFile(b *testing.B) {
} }
} }
func BenchmarkReferenceFileMap(b *testing.B) {
bytes, err := ioutil.ReadFile("benchmark.toml")
if err != nil {
b.Fatal(err)
}
b.SetBytes(int64(len(bytes)))
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
m := map[string]interface{}{}
err := toml.Unmarshal(bytes, &m)
if err != nil {
panic(err)
}
}
}
func TestReferenceFile(t *testing.T) { func TestReferenceFile(t *testing.T) {
bytes, err := ioutil.ReadFile("benchmark.toml") bytes, err := ioutil.ReadFile("benchmark.toml")
require.NoError(t, err) require.NoError(t, err)
+8
View File
@@ -172,6 +172,14 @@ func parseLocalTime(b []byte) (LocalTime, []byte, error) {
digits := 0 digits := 0
for i, c := range b[minLengthWithFrac:] { for i, c := range b[minLengthWithFrac:] {
if !isDigit(c) {
if i == 0 {
return t, nil, newDecodeError(b[i:i+1], "need at least one digit after fraction point")
}
break
}
const maxFracPrecision = 9 const maxFracPrecision = 9
if i >= maxFracPrecision { if i >= maxFracPrecision {
return t, nil, newDecodeError(b[i:i+1], "maximum precision for date time is nanosecond") return t, nil, newDecodeError(b[i:i+1], "maximum precision for date time is nanosecond")
+2 -2
View File
@@ -823,8 +823,8 @@ byteLoop:
switch { switch {
case isDigit(c): case isDigit(c):
case c == '-': case c == '-':
const offsetOfTz = 19 const minOffsetOfTz = 8
if i == offsetOfTz { if i >= minOffsetOfTz {
hasTz = true hasTz = true
} }
case c == 'T' || c == ':' || c == '.': case c == 'T' || c == ':' || c == '.':
+19 -1
View File
@@ -268,6 +268,20 @@ func TestUnmarshal(t *testing.T) {
} }
}, },
}, },
{
desc: "time.time with zone and fractional",
input: `a = 1979-05-27T00:32:00.999999-07:00`,
gen: func() test {
var v map[string]time.Time
return test{
target: &v,
expected: &map[string]time.Time{
"a": time.Date(1979, 5, 27, 0, 32, 0, 999999000, time.FixedZone("", -7*3600)),
},
}
},
},
{ {
desc: "issue 475 - space between dots in key", desc: "issue 475 - space between dots in key",
input: `fruit. color = "yellow" input: `fruit. color = "yellow"
@@ -1376,7 +1390,11 @@ func TestUnmarshalDecodeErrors(t *testing.T) {
}, },
{ {
desc: "wrong time offset separator", desc: "wrong time offset separator",
data: `a = 1979-05-27T00:32:00T07:00`, data: `a = 1979-05-27T00:32:00.-07:00`,
},
{
desc: "missing fractional with tz",
data: `a = 2021-05-09T11:22:33.99999999999`,
}, },
{ {
desc: "wrong time offset separator", desc: "wrong time offset separator",