Introduces a `config.Now` field which all components should defer to when getting the current time instead of hardcoded calls to `model.DefaultClock`.
18 lines
620 B
Go
18 lines
620 B
Go
package model
|
|
|
|
import "time"
|
|
|
|
// Clock is a type alias for a function that returns the current time as time.Time.
|
|
// It helps make time configurable within the system during testing.
|
|
//
|
|
// Any component which relies upon the current time should accept an instance to Clock as a parameter.
|
|
type Clock = func() time.Time
|
|
|
|
// Now returns the current UTC time truncated to the nearest millisecond.
|
|
// Any more precision than millisecond seems unnecessarily precise in a distributed system.
|
|
//
|
|
// It is the default clock to be used unless testing.
|
|
func Now() time.Time {
|
|
return time.Now().UTC().Truncate(time.Millisecond)
|
|
}
|