tummy

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2025 License: 0BSD Imports: 2 Imported by: 0

README

Tummy Time ⏳

License Coverage GitHub Workflow Status Go Report Card 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()

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 1 day:", tummy.Now())
// After 1 day: 1923-10-23 14:00:00.000015641 +0000 UTC

Documentation

Overview

Example
package main

import (
	"fmt"
	"time"

	"github.com/rakunlabs/tummy"
)

func main() {
	tummy.Enable()

	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
	fmt.Println(tummy.Now().Format(time.RFC3339) == time.Now().Format(time.RFC3339)) // 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 sets the global time.Now to a custom tummy.Now function.

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