decode: preserve nanosecond precision when decoding time (#626)

Co-authored-by: Thomas Pelletier <thomas@pelletier.codes>
This commit is contained in:
Cameron Moore
2021-10-17 19:43:29 -05:00
committed by GitHub
parent 76f53c857b
commit a23850f29b
5 changed files with 58 additions and 18 deletions
+19 -8
View File
@@ -37,14 +37,18 @@ func TestLocalDate_UnmarshalMarshalText(t *testing.T) {
}
func TestLocalTime_String(t *testing.T) {
d := toml.LocalTime{20, 12, 1, 2}
d := toml.LocalTime{20, 12, 1, 2, 9}
require.Equal(t, "20:12:01.000000002", d.String())
d = toml.LocalTime{20, 12, 1, 0}
d = toml.LocalTime{20, 12, 1, 0, 0}
require.Equal(t, "20:12:01", d.String())
d = toml.LocalTime{20, 12, 1, 0, 9}
require.Equal(t, "20:12:01.000000000", d.String())
d = toml.LocalTime{20, 12, 1, 100, 0}
require.Equal(t, "20:12:01.0000001", d.String())
}
func TestLocalTime_MarshalText(t *testing.T) {
d := toml.LocalTime{20, 12, 1, 2}
d := toml.LocalTime{20, 12, 1, 2, 9}
b, err := d.MarshalText()
require.NoError(t, err)
require.Equal(t, []byte("20:12:01.000000002"), b)
@@ -54,7 +58,7 @@ func TestLocalTime_UnmarshalMarshalText(t *testing.T) {
d := toml.LocalTime{}
err := d.UnmarshalText([]byte("20:12:01.000000002"))
require.NoError(t, err)
require.Equal(t, toml.LocalTime{20, 12, 1, 2}, d)
require.Equal(t, toml.LocalTime{20, 12, 1, 2, 9}, d)
err = d.UnmarshalText([]byte("what"))
require.Error(t, err)
@@ -63,10 +67,17 @@ func TestLocalTime_UnmarshalMarshalText(t *testing.T) {
require.Error(t, err)
}
func TestLocalTime_RoundTrip(t *testing.T) {
var d struct{ A toml.LocalTime }
err := toml.Unmarshal([]byte("a=20:12:01.500"), &d)
require.NoError(t, err)
require.Equal(t, "20:12:01.500", d.A.String())
}
func TestLocalDateTime_AsTime(t *testing.T) {
d := toml.LocalDateTime{
toml.LocalDate{2021, 6, 8},
toml.LocalTime{20, 12, 1, 2},
toml.LocalTime{20, 12, 1, 2, 9},
}
cast := d.AsTime(time.UTC)
require.Equal(t, time.Date(2021, time.June, 8, 20, 12, 1, 2, time.UTC), cast)
@@ -75,7 +86,7 @@ func TestLocalDateTime_AsTime(t *testing.T) {
func TestLocalDateTime_String(t *testing.T) {
d := toml.LocalDateTime{
toml.LocalDate{2021, 6, 8},
toml.LocalTime{20, 12, 1, 2},
toml.LocalTime{20, 12, 1, 2, 9},
}
require.Equal(t, "2021-06-08 20:12:01.000000002", d.String())
}
@@ -83,7 +94,7 @@ func TestLocalDateTime_String(t *testing.T) {
func TestLocalDateTime_MarshalText(t *testing.T) {
d := toml.LocalDateTime{
toml.LocalDate{2021, 6, 8},
toml.LocalTime{20, 12, 1, 2},
toml.LocalTime{20, 12, 1, 2, 9},
}
b, err := d.MarshalText()
require.NoError(t, err)
@@ -96,7 +107,7 @@ func TestLocalDateTime_UnmarshalMarshalText(t *testing.T) {
require.NoError(t, err)
require.Equal(t, toml.LocalDateTime{
toml.LocalDate{2021, 6, 8},
toml.LocalTime{20, 12, 1, 2},
toml.LocalTime{20, 12, 1, 2, 9},
}, d)
err = d.UnmarshalText([]byte("what"))