From 922fb3be75e4c043691f79a93db17386609e7792 Mon Sep 17 00:00:00 2001 From: Cary Penniman Date: Fri, 7 Jun 2024 15:28:53 -0700 Subject: [PATCH] fix: unavailable date json values --- pkg/utils/time.go | 4 ++++ pkg/utils/time_test.go | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 pkg/utils/time_test.go diff --git a/pkg/utils/time.go b/pkg/utils/time.go index 6e26693..cf3b6c2 100644 --- a/pkg/utils/time.go +++ b/pkg/utils/time.go @@ -19,6 +19,10 @@ func (ct *Time) UnmarshalJSON(b []byte) (err error) { // MarshalJSON writes a quoted string in the custom format func (ct Time) MarshalJSON() ([]byte, error) { + if ct.IsZero() { + return []byte("\"\""), nil + } + return []byte(ct.String()), nil } diff --git a/pkg/utils/time_test.go b/pkg/utils/time_test.go new file mode 100644 index 0000000..4440085 --- /dev/null +++ b/pkg/utils/time_test.go @@ -0,0 +1,18 @@ +// Copyright 2020 The Moov Authors +// Use of this source code is governed by an Apache License +// license that can be found in the LICENSE file. + +package utils + +import ( + "bytes" + "testing" +) + +func TestMarshalJSONIsZeroDate(t *testing.T) { + ct := new(Time) // create zero time + str, _ := ct.MarshalJSON() + if !bytes.Equal(str, []byte("\"\"")) { + t.Error("Expected empty string but received: \"" + string(str) + "\"\n") + } +}