tummy

package module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2026 License: 0BSD Imports: 2 Imported by: 0

README

Tummy Time ⏳

License Coverage GitHub Workflow Status Go PKG

Tummy is a Go library that provides a simple way to manipulate time in your tests.
It allows you to control the flow of time, making it easier to test time-dependent code.

go get github.com/rakunlabs/tummy

Usage

// enable tummy time manipulation, usually in the test setup
tummy.Enable()
defer tummy.Disable()

tummy.SetTime(time.Date(1919, 5, 19, 12, 0, 0, 0, time.UTC))

fmt.Println("Current time:", tummy.Now())
// Current time: 1919-05-19 12:00:00.000000119 +0000 UTC

tummy.AddDuration(2 * time.Hour)
fmt.Println("After 2 hours:", tummy.Now())
// After 2 hours: 1919-05-19 14:00:00.000019847 +0000 UTC

tummy.AddDate(4, 4, 4)
fmt.Println("After 4 years, 4 months, 4 days:", tummy.Now())
// After 4 years, 4 months, 4 days: 1923-09-23 14:00:00.000025641 +0000 UTC

Now() returns real time until Enable() is called. Disable() switches back to real time while preserving the manipulated clock's state. Pause() freezes the manipulated clock; Resume() continues from the frozen time, excluding the time spent paused. AddDate() applies calendar arithmetic to the current manipulated time, including the time zone's daylight saving rules.

Now is a function variable that defaults directly to time.Now, with no extra locking or atomic checks. Call Enable() before starting goroutines that use Now(), and Disable() after they have finished; changing Now concurrently with calls to it is not supported. Once enabled, clock reads and time manipulation operations are protected by a mutex.

The manipulated clock is shared across the process, so tests that need different clock settings should not run in parallel.

Documentation

Overview

Example
package main

import (
	"fmt"
	"time"

	"github.com/rakunlabs/tummy"
)

func main() {
	tummy.Enable()
	defer tummy.Disable()

	tummy.Resume() // it is already resuming

	tummy.SetTime(time.Date(1919, 5, 19, 12, 0, 0, 0, time.UTC))

	tummy.AddDuration(2 * time.Hour)
	fmt.Println(tummy.Now().Hour()) // 14

	tummy.AddDate(0, 0, 1)
	fmt.Println(tummy.Now().Day()) // 20

	tummy.Pause()
	tummy.SetTime(time.Date(1923, 10, 23, 12, 0, 0, 0, time.UTC))

	fmt.Println(tummy.Now().Format(time.RFC3339Nano) == "1923-10-23T12:00:00Z") // true

	tummy.Pause()
	fmt.Println(tummy.IsPaused()) // true
	tummy.Resume()

	tummy.Disable() // reset tummy.Now to time.Now
	before := time.Now()
	now := tummy.Now()
	after := time.Now()
	fmt.Println(!now.Before(before) && !now.After(after)) // true

}
Output:
14
20
true
true
true

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	Now = time.Now
)

Functions

func AddDate

func AddDate(years int, months int, days int)

AddDate adds years, months, and days to the global tummy.Now.

func AddDuration

func AddDuration(d time.Duration)

AddDuration adds a duration to the global tummy.Now.

func Disable

func Disable()

Disable resets the global tummy.Now to the original time.Now function.

func Enable

func Enable()

Enable makes Now return the manipulated time.

func IsPaused added in v0.1.1

func IsPaused() bool

IsPaused checks if the global tummy.Now is currently paused.

func Pause added in v0.1.1

func Pause()

Pause pauses the global tummy.Now, freezing the time.

func Resume added in v0.1.1

func Resume()

Resume resumes the global tummy.Now, unfreezing the time.

func SetTime

func SetTime(t time.Time)

SetTime sets the global tummy.Now to a specific time.

Types

This section is empty.

Jump to

Keyboard shortcuts

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