clock

package
v0.5.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 3, 2026 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package clock abstracts the current time so production code can use the real system clock while tests inject a controllable fake.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Clock

type Clock interface {
	Now() time.Time
}

Clock provides the current time. Production code depends on this interface, never on time.Now directly, so tests can substitute Fake. Now always returns a time.Time in UTC — callers can rely on this and don't need to call .UTC() themselves, though doing so is harmless.

type Fake

type Fake struct {
	// contains filtered or unexported fields
}

Fake is a controllable Clock for tests. Set and Advance let a test drive time deterministically instead of sleeping or tolerating a fuzzy window. Like Real, it normalizes every instant to UTC, so Clock's "Now() is always UTC" contract holds regardless of which implementation is in use.

func NewFake

func NewFake(t0 time.Time) *Fake

NewFake returns a Fake pinned to t0 (converted to UTC).

func (*Fake) Advance

func (f *Fake) Advance(d time.Duration)

Advance moves the fake's current time forward by d.

func (*Fake) Now

func (f *Fake) Now() time.Time

Now returns the fake's current time, in UTC.

func (*Fake) Set

func (f *Fake) Set(t time.Time)

Set pins the fake's current time to t (converted to UTC).

type Real

type Real struct{}

Real is the production Clock, backed by the system clock.

func NewReal

func NewReal() Real

NewReal returns a Clock backed by time.Now.

func (Real) Now

func (Real) Now() time.Time

Now returns the current system time, in UTC.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL