timekit

package
v0.42.2 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2025 License: MIT Imports: 2 Imported by: 0

README

The timekit package

The timekit package provides time.Time related helpers.

Clocks

Clock functions have the same signature as time.Time which can be used to inject deterministic clocks.

  • ClockStartingAt - returns current time with given offset.
  • ClockFixed - always returns the same time.
  • ClockDeterministic - returns time advanced by given duration no mather how fast you call it.
  • TikTak - like ClockDeterministic with duration set to 1 second.

Documentation

Overview

Package timekit provides time.Time related helpers.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ClockDeterministic

func ClockDeterministic(start time.Time, tick time.Duration) func() time.Time

ClockDeterministic returns the function with the same signature as time.Now and returning time advancing by the given tick with every call no matter how fast or slow you call it.

Example
start := time.Date(2022, 1, 1, 0, 0, 0, 0, time.UTC)
clk := ClockDeterministic(start, time.Hour)

fmt.Println(clk())
fmt.Println(clk())
fmt.Println(clk())
Output:

2022-01-01 00:00:00 +0000 UTC
2022-01-01 01:00:00 +0000 UTC
2022-01-01 02:00:00 +0000 UTC

func ClockFixed

func ClockFixed(tim time.Time) func() time.Time

ClockFixed returns the function with the same signature as time.Now which always returns the given time.

Example
clk := ClockFixed(time.Date(2022, 1, 1, 0, 0, 0, 0, time.UTC))

fmt.Println(clk())
fmt.Println(clk())
fmt.Println(clk())
Output:

2022-01-01 00:00:00 +0000 UTC
2022-01-01 00:00:00 +0000 UTC
2022-01-01 00:00:00 +0000 UTC

func ClockStartingAt

func ClockStartingAt(tim time.Time) func() time.Time

ClockStartingAt returns the function with the same signature as time.Now and returning time as if the current time was set to the given value.

func TikTak

func TikTak(start time.Time) func() time.Time

TikTak returns a deterministic clock advancing one second for each call.

Example
start := time.Date(2022, 1, 1, 0, 0, 0, 0, time.UTC)
clk := TikTak(start)

fmt.Println(clk())
fmt.Println(clk())
fmt.Println(clk())
Output:

2022-01-01 00:00:00 +0000 UTC
2022-01-01 00:00:01 +0000 UTC
2022-01-01 00:00:02 +0000 UTC

Types

This section is empty.

Jump to

Keyboard shortcuts

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