Skip to content

Commit

Permalink
Merge pull request #1582 from myxo/master
Browse files Browse the repository at this point in the history
Fix time.Time compare
  • Loading branch information
brackendawson committed Apr 1, 2024
2 parents be3fbeb + 17b83c5 commit 726249e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
8 changes: 7 additions & 1 deletion assert/assertion_compare.go
Expand Up @@ -328,7 +328,13 @@ func compare(obj1, obj2 interface{}, kind reflect.Kind) (compareResult, bool) {
timeObj2 = obj2Value.Convert(timeType).Interface().(time.Time)
}

return compare(timeObj1.UnixNano(), timeObj2.UnixNano(), reflect.Int64)
if timeObj1.Before(timeObj2) {
return compareLess, true
}
if timeObj1.Equal(timeObj2) {
return compareEqual, true
}
return compareGreater, true
}
case reflect.Slice:
{
Expand Down
1 change: 1 addition & 0 deletions assert/assertion_compare_test.go
Expand Up @@ -59,6 +59,7 @@ func TestCompare(t *testing.T) {
{less: uintptr(1), greater: uintptr(2), cType: "uintptr"},
{less: customUintptr(1), greater: customUintptr(2), cType: "uint64"},
{less: time.Now(), greater: time.Now().Add(time.Hour), cType: "time.Time"},
{less: time.Date(2024, 0, 0, 0, 0, 0, 0, time.Local), greater: time.Date(2263, 0, 0, 0, 0, 0, 0, time.Local), cType: "time.Time"},
{less: customTime(time.Now()), greater: customTime(time.Now().Add(time.Hour)), cType: "time.Time"},
{less: []byte{1, 1}, greater: []byte{1, 2}, cType: "[]byte"},
{less: customBytes([]byte{1, 1}), greater: customBytes([]byte{1, 2}), cType: "[]byte"},
Expand Down

0 comments on commit 726249e

Please sign in to comment.