testtime

pkg.go.dev

testtime provides time.Now for testing.

WARNING: This package is an experimental project.

https://play.golang.org/p/ML5nhtXLOWA

package main

import (
	"fmt"
	"time"
	_ "unsafe" // for go:linkname

	"github.com/tenntenn/testtime"
)

// replace time.Now
//go:linkname now time.Now
func now() time.Time {
	return testtime.Now()
}

func main() {
	func() {
		// set zero value
		testtime.Set(time.Time{})
		// true
		fmt.Println(time.Now().IsZero())
	}()
	// false
	fmt.Println(time.Now().IsZero())
}

GitHub

https://github.com/tenntenn/testtime