Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(civil): add IsEmpty function to time, date and datetime #4728

Merged
merged 5 commits into from Sep 28, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions civil/civil.go
Expand Up @@ -107,6 +107,11 @@ func (d1 Date) After(d2 Date) bool {
return d2.Before(d1)
}

// IsEmpty reports whether date fields are empty
codyoss marked this conversation as resolved.
Show resolved Hide resolved
func (d Date) IsEmpty() bool {
codyoss marked this conversation as resolved.
Show resolved Hide resolved
return (d.Year == 0) && (int(d.Month) == 0) && (d.Day == 0)
}

// MarshalText implements the encoding.TextMarshaler interface.
// The output is the result of d.String().
func (d Date) MarshalText() ([]byte, error) {
Expand Down Expand Up @@ -175,6 +180,11 @@ func (t Time) IsValid() bool {
return TimeOf(tm) == t
}

// IsEmpty reports whether time fields are empty
func (t Time) IsEmpty() bool {
return (t.Hour == 0) && (t.Minute == 0) && (t.Second == 0) && (t.Nanosecond == 0)
}

// MarshalText implements the encoding.TextMarshaler interface.
// The output is the result of t.String().
func (t Time) MarshalText() ([]byte, error) {
Expand Down Expand Up @@ -262,6 +272,11 @@ func (dt1 DateTime) After(dt2 DateTime) bool {
return dt2.Before(dt1)
}

// IsEmpty reports whether datetime fields are empty
func (dt DateTime) IsEmpty() bool {
return dt.Date.IsEmpty() && dt.Time.IsEmpty()
}

// MarshalText implements the encoding.TextMarshaler interface.
// The output is the result of dt.String().
func (dt DateTime) MarshalText() ([]byte, error) {
Expand Down
58 changes: 58 additions & 0 deletions civil/civil_test.go
Expand Up @@ -193,6 +193,24 @@ func TestDateAfter(t *testing.T) {
}
}

func TestDateIsEmpty(t *testing.T) {
for _, test := range []struct {
date Date
want bool
}{
{Date{2000, 2, 29}, false},
{Date{10000, 12, 31}, false},
{Date{-1, 0, 0}, false},
{Date{0, 0, 0}, true},
{Date{}, true},
} {
got := test.date.IsEmpty()
if got != test.want {
t.Errorf("%#v: got %t, want %t", test.date, got, test.want)
}
}
}

func TestTimeToString(t *testing.T) {
for _, test := range []struct {
str string
Expand Down Expand Up @@ -260,6 +278,24 @@ func TestTimeIsValid(t *testing.T) {
}
}

func TestTimeIsEmpty(t *testing.T) {
for _, test := range []struct {
time Time
want bool
}{
{Time{0, 0, 0, 0}, true},
{Time{}, true},
{Time{0, 0, 0, 1}, false},
{Time{-1, 0, 0, 0}, false},
{Time{0, -1, 0, 0}, false},
} {
got := test.time.IsEmpty()
if got != test.want {
t.Errorf("%#v: got %t, want %t", test.time, got, test.want)
}
}
}

func TestDateTimeToString(t *testing.T) {
for _, test := range []struct {
str string
Expand Down Expand Up @@ -383,6 +419,28 @@ func TestDateTimeAfter(t *testing.T) {
}
}

func TestDateTimeIsEmpty(t *testing.T) {
for _, test := range []struct {
dt DateTime
want bool
}{
{DateTime{Date{2016, 3, 20}, Time{0, 0, 0, 0}}, false},
{DateTime{Date{}, Time{5, 44, 20, 0}}, false},
{DateTime{Date{2016, 3, 20}, Time{}}, false},
{DateTime{Date{0, 0, 0}, Time{5, 16, 47, 2}}, false},
{DateTime{Date{2021, 9, 5}, Time{9, 30, 51, 6}}, false},
{DateTime{Date{}, Time{}}, true},
{DateTime{Date{0, 0, 0}, Time{0, 0, 0, 0}}, true},
{DateTime{Date{}, Time{0, 0, 0, 0}}, true},
{DateTime{Date{0, 0, 0}, Time{}}, true},
} {
got := test.dt.IsEmpty()
if got != test.want {
t.Errorf("%#v: got %t, want %t", test.dt, got, test.want)
}
}
}

func TestMarshalJSON(t *testing.T) {
for _, test := range []struct {
value interface{}
Expand Down