Created
March 15, 2019 10:51
-
-
Save rsky/ea8599d089f3c86ca78b2503b4ed9467 to your computer and use it in GitHub Desktop.
clock for testing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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