Skip to content

Commit 88bfa64

Browse files
authored
feat(civil): add IsEmpty function to time, date and datetime (#4728)
Fixes: #4727
1 parent 37ee2d9 commit 88bfa64

2 files changed

Lines changed: 73 additions & 0 deletions

File tree

civil/civil.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,11 @@ func (d1 Date) After(d2 Date) bool {
107107
return d2.Before(d1)
108108
}
109109

110+
// IsZero reports whether date fields are set to their default value.
111+
func (d Date) IsZero() bool {
112+
return (d.Year == 0) && (int(d.Month) == 0) && (d.Day == 0)
113+
}
114+
110115
// MarshalText implements the encoding.TextMarshaler interface.
111116
// The output is the result of d.String().
112117
func (d Date) MarshalText() ([]byte, error) {
@@ -175,6 +180,11 @@ func (t Time) IsValid() bool {
175180
return TimeOf(tm) == t
176181
}
177182

183+
// IsZero reports whether time fields are set to their default value.
184+
func (t Time) IsZero() bool {
185+
return (t.Hour == 0) && (t.Minute == 0) && (t.Second == 0) && (t.Nanosecond == 0)
186+
}
187+
178188
// MarshalText implements the encoding.TextMarshaler interface.
179189
// The output is the result of t.String().
180190
func (t Time) MarshalText() ([]byte, error) {
@@ -262,6 +272,11 @@ func (dt1 DateTime) After(dt2 DateTime) bool {
262272
return dt2.Before(dt1)
263273
}
264274

275+
// IsZero reports whether datetime fields are set to their default value.
276+
func (dt DateTime) IsZero() bool {
277+
return dt.Date.IsZero() && dt.Time.IsZero()
278+
}
279+
265280
// MarshalText implements the encoding.TextMarshaler interface.
266281
// The output is the result of dt.String().
267282
func (dt DateTime) MarshalText() ([]byte, error) {

civil/civil_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,24 @@ func TestDateAfter(t *testing.T) {
193193
}
194194
}
195195

196+
func TestDateIsZero(t *testing.T) {
197+
for _, test := range []struct {
198+
date Date
199+
want bool
200+
}{
201+
{Date{2000, 2, 29}, false},
202+
{Date{10000, 12, 31}, false},
203+
{Date{-1, 0, 0}, false},
204+
{Date{0, 0, 0}, true},
205+
{Date{}, true},
206+
} {
207+
got := test.date.IsZero()
208+
if got != test.want {
209+
t.Errorf("%#v: got %t, want %t", test.date, got, test.want)
210+
}
211+
}
212+
}
213+
196214
func TestTimeToString(t *testing.T) {
197215
for _, test := range []struct {
198216
str string
@@ -260,6 +278,24 @@ func TestTimeIsValid(t *testing.T) {
260278
}
261279
}
262280

281+
func TestTimeIsZero(t *testing.T) {
282+
for _, test := range []struct {
283+
time Time
284+
want bool
285+
}{
286+
{Time{0, 0, 0, 0}, true},
287+
{Time{}, true},
288+
{Time{0, 0, 0, 1}, false},
289+
{Time{-1, 0, 0, 0}, false},
290+
{Time{0, -1, 0, 0}, false},
291+
} {
292+
got := test.time.IsZero()
293+
if got != test.want {
294+
t.Errorf("%#v: got %t, want %t", test.time, got, test.want)
295+
}
296+
}
297+
}
298+
263299
func TestDateTimeToString(t *testing.T) {
264300
for _, test := range []struct {
265301
str string
@@ -383,6 +419,28 @@ func TestDateTimeAfter(t *testing.T) {
383419
}
384420
}
385421

422+
func TestDateTimeIsZero(t *testing.T) {
423+
for _, test := range []struct {
424+
dt DateTime
425+
want bool
426+
}{
427+
{DateTime{Date{2016, 3, 20}, Time{0, 0, 0, 0}}, false},
428+
{DateTime{Date{}, Time{5, 44, 20, 0}}, false},
429+
{DateTime{Date{2016, 3, 20}, Time{}}, false},
430+
{DateTime{Date{0, 0, 0}, Time{5, 16, 47, 2}}, false},
431+
{DateTime{Date{2021, 9, 5}, Time{9, 30, 51, 6}}, false},
432+
{DateTime{Date{}, Time{}}, true},
433+
{DateTime{Date{0, 0, 0}, Time{0, 0, 0, 0}}, true},
434+
{DateTime{Date{}, Time{0, 0, 0, 0}}, true},
435+
{DateTime{Date{0, 0, 0}, Time{}}, true},
436+
} {
437+
got := test.dt.IsZero()
438+
if got != test.want {
439+
t.Errorf("%#v: got %t, want %t", test.dt, got, test.want)
440+
}
441+
}
442+
}
443+
386444
func TestMarshalJSON(t *testing.T) {
387445
for _, test := range []struct {
388446
value interface{}

0 commit comments

Comments
 (0)