From bb3f3fd50fb2bfba9106f4779cbb2afe2108bfc5 Mon Sep 17 00:00:00 2001 From: anihex Date: Thu, 24 Jan 2019 14:45:14 +0100 Subject: [PATCH] Added IsZero for null.Time --- time.go | 5 +++++ time_test.go | 12 ++++++++++++ 2 files changed, 17 insertions(+) diff --git a/time.go b/time.go index d9bd453..0380dd8 100644 --- a/time.go +++ b/time.go @@ -95,6 +95,11 @@ func (t Time) Ptr() *time.Time { return &t.Time } +// IsZero returns true for an invalid Time's value, for potential future omitempty support. +func (t Time) IsZero() bool { + return !t.Valid +} + // Scan implements the Scanner interface. func (t *Time) Scan(value interface{}) error { var err error diff --git a/time_test.go b/time_test.go index 3e1af27..b9dc7a3 100644 --- a/time_test.go +++ b/time_test.go @@ -114,6 +114,18 @@ func TestTimePointer(t *testing.T) { } } +func TestTimeIsZero(t *testing.T) { + ti := TimeFrom(time.Now()) + if ti.IsZero() { + t.Errorf("IsZero() should be false") + } + + null := TimeFromPtr(nil) + if !null.IsZero() { + t.Errorf("IsZero() should be true") + } +} + func TestTimeScanValue(t *testing.T) { var ti Time err := ti.Scan(timeValue)