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): before and after methods for Time #5703

Merged
merged 5 commits into from
May 24, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions civil/civil.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,26 @@ func (t Time) IsZero() bool {
return (t.Hour == 0) && (t.Minute == 0) && (t.Second == 0) && (t.Nanosecond == 0)
}

// Before reports whether t1 occurs before t2.
func (t1 Time) Before(t2 Time) bool {
quartzmo marked this conversation as resolved.
Show resolved Hide resolved
if t1.Hour != t2.Hour {
return t1.Hour < t2.Hour
}
if t1.Minute != t2.Minute {
return t1.Minute < t2.Minute
}
if t1.Second != t2.Second {
return t1.Second < t2.Second
}

return t1.Nanosecond < t2.Nanosecond
}

// After reports whether t1 occurs after t2.
func (t1 Time) After(t2 Time) bool {
quartzmo marked this conversation as resolved.
Show resolved Hide resolved
return t2.Before(t1)
}

// MarshalText implements the encoding.TextMarshaler interface.
// The output is the result of t.String().
func (t Time) MarshalText() ([]byte, error) {
Expand Down
32 changes: 32 additions & 0 deletions civil/civil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,38 @@ func TestTimeIsZero(t *testing.T) {
}
}

func TestTimeBefore(t *testing.T) {
for _, test := range []struct {
t1, t2 Time
want bool
}{
{Time{12, 0, 0, 0}, Time{14, 0, 0, 0}, true},
{Time{12, 20, 0, 0}, Time{12, 30, 0, 0}, true},
{Time{12, 20, 10, 0}, Time{12, 20, 20, 0}, true},
{Time{12, 20, 10, 5}, Time{12, 20, 10, 10}, true},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add a test case for equal times: {Time{12, 20, 10, 5}, Time{12, 20, 10, 5}, false},

} {
if got := test.t1.Before(test.t2); got != test.want {
t.Errorf("%v.Before(%v): got %t, want %t", test.t1, test.t2, got, test.want)
}
}
}

func TestTimeAfter(t *testing.T) {
for _, test := range []struct {
t1, t2 Time
want bool
}{
{Time{12, 0, 0, 0}, Time{14, 0, 0, 0}, false},
{Time{12, 20, 0, 0}, Time{12, 30, 0, 0}, false},
{Time{12, 20, 10, 0}, Time{12, 20, 20, 0}, false},
{Time{12, 20, 10, 5}, Time{12, 20, 10, 10}, false},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add a test case for equal times: {Time{12, 20, 10, 5}, Time{12, 20, 10, 5}, false},

} {
if got := test.t1.After(test.t2); got != test.want {
t.Errorf("%v.Before(%v): got %t, want %t", test.t1, test.t2, got, test.want)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before should be After in this message.

}
}
}

func TestDateTimeToString(t *testing.T) {
for _, test := range []struct {
str string
Expand Down