diff --git a/fs/parsetime.go b/fs/parsetime.go index 7e762451d..504232b20 100644 --- a/fs/parsetime.go +++ b/fs/parsetime.go @@ -81,6 +81,11 @@ func (t *Time) UnmarshalJSON(in []byte) error { return t.Set(s) } +// MarshalJSON marshals as a time.Time value +func (t Time) MarshalJSON() ([]byte, error) { + return json.Marshal(time.Time(t)) +} + // Scan implements the fmt.Scanner interface func (t *Time) Scan(s fmt.ScanState, ch rune) error { token, err := s.Token(true, func(rune) bool { return true }) diff --git a/fs/parsetime_test.go b/fs/parsetime_test.go index 21d34c1ee..796217b5b 100644 --- a/fs/parsetime_test.go +++ b/fs/parsetime_test.go @@ -153,3 +153,23 @@ func TestParseTimeUnmarshalJSON(t *testing.T) { assert.Equal(t, Time(test.want), parsedTime, test.in) } } + +func TestParseTimeMarshalJSON(t *testing.T) { + for _, test := range []struct { + in time.Time + want string + err bool + }{ + {time.Time{}, `"0001-01-01T00:00:00Z"`, false}, + {time.Date(2022, 03, 26, 17, 48, 19, 0, time.UTC), `"2022-03-26T17:48:19Z"`, false}, + } { + gotBytes, err := json.Marshal(test.in) + got := string(gotBytes) + if test.err { + require.Error(t, err, test.in) + } else { + require.NoError(t, err, test.in) + } + assert.Equal(t, test.want, got, test.in) + } +}