@@ -9,13 +9,17 @@ import (
9
9
"log"
10
10
"reflect"
11
11
"strings"
12
+ "time"
12
13
)
13
14
14
15
var (
15
16
// FloatPrecision is the number of decimal places to round float values
16
17
// to when comparing.
17
18
FloatPrecision = 10
18
19
20
+ // TimePrecision is a precision used for time.Time.Truncate(), if it is non-zero.
21
+ TimePrecision time.Duration
22
+
19
23
// MaxDiff specifies the maximum number of differences to return.
20
24
MaxDiff = 10
21
25
@@ -79,7 +83,11 @@ type cmp struct {
79
83
flag map [byte ]bool
80
84
}
81
85
82
- var errorType = reflect .TypeOf ((* error )(nil )).Elem ()
86
+ var (
87
+ errorType = reflect .TypeOf ((* error )(nil )).Elem ()
88
+ timeType = reflect .TypeOf (time.Time {})
89
+ durationType = reflect .TypeOf (time .Nanosecond )
90
+ )
83
91
84
92
// Equal compares variables a and b, recursing into their structure up to
85
93
// MaxDepth levels deep (if greater than zero), and returns a list of differences,
@@ -221,6 +229,21 @@ func (c *cmp) equals(a, b reflect.Value, level int) {
221
229
Iterate through the fields (FirstName, LastName), recurse into their values.
222
230
*/
223
231
232
+ if TimePrecision > 0 {
233
+ switch aType {
234
+ case timeType , durationType :
235
+ aFunc := a .MethodByName ("Truncate" )
236
+ bFunc := a .MethodByName ("Truncate" )
237
+
238
+ if aFunc .CanInterface () && bFunc .CanInterface () {
239
+ precision := reflect .ValueOf (TimePrecision )
240
+
241
+ a = aFunc .Call ([]reflect.Value {precision })[0 ]
242
+ b = bFunc .Call ([]reflect.Value {precision })[0 ]
243
+ }
244
+ }
245
+ }
246
+
224
247
// Types with an Equal() method, like time.Time, only if struct field
225
248
// is exported (CanInterface)
226
249
if eqFunc := a .MethodByName ("Equal" ); eqFunc .IsValid () && eqFunc .CanInterface () {
0 commit comments