Skip to content

Commit

Permalink
Add Before method to Time
Browse files Browse the repository at this point in the history
  • Loading branch information
ribice committed Jun 25, 2020
1 parent 86ef269 commit c4e0118
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 10 deletions.
15 changes: 10 additions & 5 deletions time.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,18 @@ func (t Time) ToDate() time.Time {

// After checks if instance of t is after tm
func (t Time) After(tm Time) bool {
if t.Hour > tm.Hour {
return true
if t.Hour == tm.Hour{
return t.Minute > tm.Minute
}
if t.Hour < tm.Hour{
return false
return t.Hour > tm.Hour
}

// After checks if instance of t is before tm
func (t Time) Before(tm Time) bool {
if t.Hour == tm.Hour{
return t.Minute < tm.Minute
}
return t.Minute > tm.Minute
return t.Hour < tm.Hour
}

// Subtract returns difference between t and t2 in minutes
Expand Down
56 changes: 51 additions & 5 deletions time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,13 @@ func TestAfter(t *testing.T) {
isAfter bool
}{
{
name: "Hours after",
name: "Hour after",
t1: Time{23, 59, true},
t2: Time{22, 59, true},
isAfter: true,
},
{
name: "Minutes after",
name: "Minute after",
t1: Time{23, 59, true},
t2: Time{23, 45, true},
isAfter: true,
Expand All @@ -109,13 +109,13 @@ func TestAfter(t *testing.T) {
isAfter: false,
},
{
name: "Before",
t1: Time{23, 59, true},
name: "Hour before",
t1: Time{22, 59, true},
t2: Time{23, 59, true},
isAfter: false,
},
{
name: "Before",
name: "Minute before",
t1: Time{11, 59, true},
t2: Time{23, 59, true},
isAfter: false,
Expand All @@ -130,6 +130,52 @@ func TestAfter(t *testing.T) {
}
}

func TestBefore(t *testing.T) {
cases := []struct {
name string
t1, t2 Time
isBefore bool
}{
{
name: "Hour after",
t1: Time{23, 59, true},
t2: Time{22, 59, true},
isBefore: false,
},
{
name: "Minute after",
t1: Time{23, 59, true},
t2: Time{23, 45, true},
isBefore: false,
},
{
name: "Equal",
t1: Time{23, 59, true},
t2: Time{23, 59, true},
isBefore: false,
},
{
name: "Hour before",
t1: Time{22, 59, true},
t2: Time{23, 59, true},
isBefore: true,
},
{
name: "Minute before",
t1: Time{11, 59, true},
t2: Time{23, 59, true},
isBefore: true,
},
}
for _, tt := range cases {
t.Run(tt.name, func(t *testing.T) {
if tt.t1.Before(tt.t2) != tt.isBefore {
t.Errorf("expected isBefore %v, got %v", tt.isBefore, tt.t1.Before(tt.t2))
}
})
}
}

func TestSubtract(t *testing.T) {
cases := []struct {
name string
Expand Down

0 comments on commit c4e0118

Please sign in to comment.