From 31caa019fa96b60bb22b02426d8db60835b47c0f Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Wed, 3 May 2023 15:46:26 +0100 Subject: [PATCH] rc: fix output of Time values in options/get Before this change these were output as `{}` after this change they are output as time strings `"2022-03-26T17:48:19Z"` in standard javascript format. --- fs/parsetime.go | 5 +++++ fs/parsetime_test.go | 20 ++++++++++++++++++++ 2 files changed, 25 insertions(+) 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) + } +}