TimeComparator #42

Merged
RichardHightower merged 2 commits from master into development 2017-03-03 22:02:03 +00:00
RichardHightower commented 2016-12-20 18:38:12 +00:00 (Migrated from github.com)

We added a time comparison. We use TreeMaps with times extensively. The guess is that others do this is as well.

Small addition

// TimeComparator provides a basic comparison on time.Time
func TimeComparator(a, b interface{}) int {
	aAsserted := a.(time.Time)
	bAsserted := b.(time.Time)

	switch {
	case aAsserted.After(bAsserted) :
		return 1
	case aAsserted.Before(bAsserted):
		return -1
	default:
		return 0
	}
}

With a test case.

func TestTimeComparator(t *testing.T) {

	now := time.Now()

	// i1,i2,expected
	tests := [][]interface{}{
		{now, now, 0},
		{now.Add(24 * 7 * 2 * time.Hour), now, 1},
		{now, now.Add(24 * 7 * 2 * time.Hour), -1},
	}

	for _, test := range tests {
		actual := TimeComparator(test[0], test[1])
		expected := test[2]
		if actual != expected {
			t.Errorf("Got %v expected %v", actual, expected)
		}
	}
}
We added a time comparison. We use TreeMaps with times extensively. The guess is that others do this is as well. #### Small addition ```golang // TimeComparator provides a basic comparison on time.Time func TimeComparator(a, b interface{}) int { aAsserted := a.(time.Time) bAsserted := b.(time.Time) switch { case aAsserted.After(bAsserted) : return 1 case aAsserted.Before(bAsserted): return -1 default: return 0 } } ``` With a test case. ```golang func TestTimeComparator(t *testing.T) { now := time.Now() // i1,i2,expected tests := [][]interface{}{ {now, now, 0}, {now.Add(24 * 7 * 2 * time.Hour), now, 1}, {now, now.Add(24 * 7 * 2 * time.Hour), -1}, } for _, test := range tests { actual := TimeComparator(test[0], test[1]) expected := test[2] if actual != expected { t.Errorf("Got %v expected %v", actual, expected) } } } ```
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
golibs/gms-gods!42
No description provided.