Skip to content

Commit

Permalink
Local time support (#318)
Browse files Browse the repository at this point in the history
  • Loading branch information
pelletier committed Oct 25, 2019
1 parent 3a4d7af commit 5e74bb9
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 5 deletions.
3 changes: 2 additions & 1 deletion marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ const (
var timeType = reflect.TypeOf(time.Time{})
var marshalerType = reflect.TypeOf(new(Marshaler)).Elem()
var localDateType = reflect.TypeOf(LocalDate{})
var localTimeType = reflect.TypeOf(LocalTime{})
var localDateTimeType = reflect.TypeOf(LocalDateTime{})

// Check if the given marshal type maps to a Tree primitive
Expand All @@ -87,7 +88,7 @@ func isPrimitive(mtype reflect.Type) bool {
case reflect.String:
return true
case reflect.Struct:
return mtype == timeType || mtype == localDateType || mtype == localDateTimeType || isCustomMarshaler(mtype)
return mtype == timeType || mtype == localDateType || mtype == localDateTimeType || mtype == localTimeType || isCustomMarshaler(mtype)
default:
return false
}
Expand Down
110 changes: 106 additions & 4 deletions marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1959,16 +1959,16 @@ func TestUnmarshalLocalDateTime(t *testing.T) {
t.Errorf("expected day %d, got %d", example.out.Date.Day, obj.Date.Day())
}
if obj.Date.Hour() != example.out.Time.Hour {
t.Errorf("expected day %d, got %d", example.out.Time.Hour, obj.Date.Hour())
t.Errorf("expected hour %d, got %d", example.out.Time.Hour, obj.Date.Hour())
}
if obj.Date.Minute() != example.out.Time.Minute {
t.Errorf("expected day %d, got %d", example.out.Time.Minute, obj.Date.Minute())
t.Errorf("expected minute %d, got %d", example.out.Time.Minute, obj.Date.Minute())
}
if obj.Date.Second() != example.out.Time.Second {
t.Errorf("expected day %d, got %d", example.out.Time.Second, obj.Date.Second())
t.Errorf("expected second %d, got %d", example.out.Time.Second, obj.Date.Second())
}
if obj.Date.Nanosecond() != example.out.Time.Nanosecond {
t.Errorf("expected day %d, got %d", example.out.Time.Nanosecond, obj.Date.Nanosecond())
t.Errorf("expected nanoseconds %d, got %d", example.out.Time.Nanosecond, obj.Date.Nanosecond())
}
})
}
Expand Down Expand Up @@ -2038,3 +2038,105 @@ func TestMarshalLocalDateTime(t *testing.T) {
})
}
}

func TestUnmarshalLocalTime(t *testing.T) {
examples := []struct {
name string
in string
out LocalTime
}{
{
name: "normal",
in: "07:32:00",
out: LocalTime{
Hour: 7,
Minute: 32,
Second: 0,
Nanosecond: 0,
},
},
{
name: "with nanoseconds",
in: "00:32:00.999999",
out: LocalTime{
Hour: 0,
Minute: 32,
Second: 0,
Nanosecond: 999999000,
},
},
}

for i, example := range examples {
toml := fmt.Sprintf(`Time = %s`, example.in)

t.Run(fmt.Sprintf("ToLocalTime_%d_%s", i, example.name), func(t *testing.T) {
type dateStruct struct {
Time LocalTime
}

var obj dateStruct

err := Unmarshal([]byte(toml), &obj)

if err != nil {
t.Fatal(err)
}

if obj.Time != example.out {
t.Errorf("expected '%s', got '%s'", example.out, obj.Time)
}
})
}
}

func TestMarshalLocalTime(t *testing.T) {
type timeStruct struct {
Time LocalTime
}

examples := []struct {
name string
in LocalTime
out string
}{
{
name: "normal",
out: "Time = 07:32:00\n",
in: LocalTime{
Hour: 7,
Minute: 32,
Second: 0,
Nanosecond: 0,
}},
{
name: "with nanoseconds",
out: "Time = 00:32:00.999999000\n",
in: LocalTime{
Hour: 0,
Minute: 32,
Second: 0,
Nanosecond: 999999000,
},
},
}

for i, example := range examples {
t.Run(fmt.Sprintf("%d_%s", i, example.name), func(t *testing.T) {
obj := timeStruct{
Time: example.in,
}
b, err := Marshal(obj)

if err != nil {
t.Fatalf("unexpected error: %v", err)
}

got := string(b)

if got != example.out {
t.Errorf("expected '%s', got '%s'", example.out, got)
}
})
}
}
2 changes: 2 additions & 0 deletions tomltree_write.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ func tomlValueStringRepresentation(v interface{}, indent string, arraysOneElemen
return value.String(), nil
case LocalDateTime:
return value.String(), nil
case LocalTime:
return value.String(), nil
case nil:
return "", nil
}
Expand Down

0 comments on commit 5e74bb9

Please sign in to comment.