diff --git a/README.md b/README.md index 9bb50e6..1afbecd 100644 --- a/README.md +++ b/README.md @@ -1253,7 +1253,7 @@ This takes a while, so test within sub-packages: Biggest contribution towards this library is to use it and give us feedback for further improvements and additions. -For direct contributions, _pull request_ into master or ask to become a contributor. +For direct contributions, _pull request_ into development branch or ask to become a contributor. Coding style: diff --git a/utils/comparator.go b/utils/comparator.go index 98d6e3d..6a9afbf 100644 --- a/utils/comparator.go +++ b/utils/comparator.go @@ -4,6 +4,8 @@ package utils +import "time" + // Comparator will make type assertion (see IntComparator for example), // which will panic if a or b are not of the asserted type. // @@ -232,3 +234,18 @@ func RuneComparator(a, b interface{}) int { return 0 } } + +// 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 + } +} diff --git a/utils/comparator_test.go b/utils/comparator_test.go index 12849f8..40efbd3 100644 --- a/utils/comparator_test.go +++ b/utils/comparator_test.go @@ -6,6 +6,7 @@ package utils import ( "testing" + "time" ) func TestIntComparator(t *testing.T) { @@ -53,6 +54,26 @@ func TestStringComparator(t *testing.T) { } } +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) + } + } +} + func TestCustomComparator(t *testing.T) { type Custom struct {