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// 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 UTCNow() 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.