TruncTime operator compares time.Time (or assignable) values
after truncating them to the optional trunc duration. See
time.Time.Truncate for details about the truncation.
If trunc is missing, it defaults to 0.
During comparison, location does not matter as time.Time.Equal
method is used behind the scenes: a time instant in two different
locations is the same time instant.
Whatever the trunc value is, the monotonic clock is stripped
before the comparison against expectedTime.
t:=&testing.T{}dateToTime:=func(strstring)time.Time{t,err:=time.Parse(time.RFC3339Nano,str)iferr!=nil{panic(err)}returnt}got:=dateToTime("2018-05-01T12:45:53.123456789Z")// Compare dates ignoring nanoseconds and monotonic partsexpected:=dateToTime("2018-05-01T12:45:53Z")ok:=td.Cmp(t,got,td.TruncTime(expected,time.Second),"checks date %v, truncated to the second",got)fmt.Println(ok)// Compare dates ignoring time and so monotonic partsexpected=dateToTime("2018-05-01T11:22:33.444444444Z")ok=td.Cmp(t,got,td.TruncTime(expected,24*time.Hour),"checks date %v, truncated to the day",got)fmt.Println(ok)// Compare dates exactly but ignoring monotonic partexpected=dateToTime("2018-05-01T12:45:53.123456789Z")ok=td.Cmp(t,got,td.TruncTime(expected),"checks date %v ignoring monotonic part",got)fmt.Println(ok)// Output:// true// true// true
args… are optional and allow to name the test. This name is
used in case of failure to qualify the test. If len(args) > 1 and
the first item of args is a string and contains a ‘%’ rune then
fmt.Fprintf is used to compose the name, else args are passed to
fmt.Fprint. Do not forget it is the name of the test, not the
reason of a potential failure.
t:=&testing.T{}dateToTime:=func(strstring)time.Time{t,err:=time.Parse(time.RFC3339Nano,str)iferr!=nil{panic(err)}returnt}got:=dateToTime("2018-05-01T12:45:53.123456789Z")// Compare dates ignoring nanoseconds and monotonic partsexpected:=dateToTime("2018-05-01T12:45:53Z")ok:=td.CmpTruncTime(t,got,expected,time.Second,"checks date %v, truncated to the second",got)fmt.Println(ok)// Compare dates ignoring time and so monotonic partsexpected=dateToTime("2018-05-01T11:22:33.444444444Z")ok=td.CmpTruncTime(t,got,expected,24*time.Hour,"checks date %v, truncated to the day",got)fmt.Println(ok)// Compare dates exactly but ignoring monotonic partexpected=dateToTime("2018-05-01T12:45:53.123456789Z")ok=td.CmpTruncTime(t,got,expected,0,"checks date %v ignoring monotonic part",got)fmt.Println(ok)// Output:// true// true// true
TruncTime optional parameter trunc is here mandatory.
0 value should be passed to mimic its absence in
original TruncTime call.
Returns true if the test is OK, false if it fails.
args… are optional and allow to name the test. This name is
used in case of failure to qualify the test. If len(args) > 1 and
the first item of args is a string and contains a ‘%’ rune then
fmt.Fprintf is used to compose the name, else args are passed to
fmt.Fprint. Do not forget it is the name of the test, not the
reason of a potential failure.
t:=td.NewT(&testing.T{})dateToTime:=func(strstring)time.Time{t,err:=time.Parse(time.RFC3339Nano,str)iferr!=nil{panic(err)}returnt}got:=dateToTime("2018-05-01T12:45:53.123456789Z")// Compare dates ignoring nanoseconds and monotonic partsexpected:=dateToTime("2018-05-01T12:45:53Z")ok:=t.TruncTime(got,expected,time.Second,"checks date %v, truncated to the second",got)fmt.Println(ok)// Compare dates ignoring time and so monotonic partsexpected=dateToTime("2018-05-01T11:22:33.444444444Z")ok=t.TruncTime(got,expected,24*time.Hour,"checks date %v, truncated to the day",got)fmt.Println(ok)// Compare dates exactly but ignoring monotonic partexpected=dateToTime("2018-05-01T12:45:53.123456789Z")ok=t.TruncTime(got,expected,0,"checks date %v ignoring monotonic part",got)fmt.Println(ok)// Output:// true// true// true