TruncTime

func TruncTime(expectedTime any, trunc ...time.Duration) TestDeep

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.

gotDate := time.Date(2018, time.March, 9, 1, 2, 3, 999999999, time.UTC).
  In(time.FixedZone("UTC+2", 2))

expected := time.Date(2018, time.March, 9, 1, 2, 3, 0, time.UTC)

td.Cmp(t, gotDate, td.TruncTime(expected))              // fails, ns differ
td.Cmp(t, gotDate, td.TruncTime(expected, time.Second)) // succeeds

TypeBehind method returns the reflect.Type of expectedTime.

See also TruncTime godoc.

Example

Base example

CmpTruncTime shortcut

func CmpTruncTime(t TestingT, got, expectedTime any, trunc time.Duration, args ...any) bool

CmpTruncTime is a shortcut for:

td.Cmp(t, got, td.TruncTime(expectedTime, trunc), args...)

See above for details.

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.

If t is a *T then its Config field is inherited.

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.

See also CmpTruncTime godoc.

Example

Base example

T.TruncTime shortcut

func (t *T) TruncTime(got, expectedTime any, trunc time.Duration, args ...any) bool

TruncTime is a shortcut for:

t.Cmp(got, td.TruncTime(expectedTime, trunc), args...)

See above for details.

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.

See also T.TruncTime godoc.

Example

Base example