Skip to content

Instantly share code, notes, and snippets.

@rsky
Created March 15, 2019 10:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rsky/ea8599d089f3c86ca78b2503b4ed9467 to your computer and use it in GitHub Desktop.
Save rsky/ea8599d089f3c86ca78b2503b4ed9467 to your computer and use it in GitHub Desktop.
clock for testing
package clock
import (
"flag"
"time"
)
type NowFunc func() time.Time
var nowFunc NowFunc
func init() {
ResetNowFunc()
}
func Now() time.Time {
return nowFunc()
}
func ForceNow(t time.Time) {
SetNowFunc(func() time.Time {
return t
})
}
func SetNowFunc(f NowFunc) {
if flag.Lookup("test.v") == nil {
panic("not testing!")
}
nowFunc = f
}
func ResetNowFunc() {
nowFunc = time.Now
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment