clock

package
v0.48.0 Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package clock provides a small Clock abstraction so time-dependent logic (notably the per-day spend-cap reset) can be driven by a fake clock in tests. Production code uses RealClock (UTC); tests inject FakeClock and advance it across day boundaries deterministically.

The rest of the codebase calls time.Now() directly; this package is intentionally narrow and only adopted where day-boundary behaviour must be tested without sleeping or racing the wall clock.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DayKey

func DayKey(t time.Time) string

DayKey returns the UTC calendar-day key (YYYY-MM-DD) for t. This is the canonical partition key for the daily spend ledger.

Types

type Clock

type Clock interface {
	// Now returns the current time. Implementations SHOULD return UTC
	// so callers can derive a stable YYYY-MM-DD day key.
	Now() time.Time
}

Clock is the minimal time source consumed by the spend-cap engine.

var Default Clock = RealClock{}

Default is the package-level Clock used when no clock is injected.

type FakeClock

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

FakeClock is a deterministic Clock for tests. The zero value is not usable — construct with NewFakeClock.

func NewFakeClock

func NewFakeClock(t time.Time) *FakeClock

NewFakeClock returns a FakeClock pinned to t (normalised to UTC).

func (*FakeClock) Advance

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

Advance moves the fake clock forward by d.

func (*FakeClock) Now

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

Now returns the fake clock's current time.

func (*FakeClock) SetTime

func (f *FakeClock) SetTime(t time.Time)

SetTime pins the fake clock to t (normalised to UTC).

type RealClock

type RealClock struct{}

RealClock is the production Clock; it returns the wall-clock time in UTC so day keys are stable regardless of host timezone.

func (RealClock) Now

func (RealClock) Now() time.Time

Now returns time.Now().UTC().

Jump to

Keyboard shortcuts

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