package isolatedjob import "time" // Clock gives the controller two readings. Wall is the calendar time, which // can jump when the host clock is set. Mono is time elapsed since some fixed // start; it never decreases, so deadlines and timeouts use it. type Clock interface { Wall() time.Time Mono() time.Duration } // SystemClock returns a Clock on this host's clocks. Mono is measured with // Go's monotonic reading from the moment SystemClock is called. func SystemClock() Clock { return newSystemClock(time.Now, time.Since) } // newSystemClock is SystemClock with its two clock sources passed in, so a // test can move the wall clock back while elapsed time moves forward. func newSystemClock(now func() time.Time, since func(time.Time) time.Duration) systemClock { return systemClock{start: now(), now: now, since: since} } type systemClock struct { start time.Time // keeps Go's monotonic reading now func() time.Time // the wall clock since func(time.Time) time.Duration // elapsed time by the monotonic reading } // Wall strips the monotonic reading, so a Wall value never stands in for Mono. func (c systemClock) Wall() time.Time { return c.now().Round(0) } // Mono never reads the wall clock. func (c systemClock) Mono() time.Duration { return c.since(c.start) }