Skip to content

Instantly share code, notes, and snippets.

@karlseguin
Created June 9, 2013 07:28
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karlseguin/5742394 to your computer and use it in GitHub Desktop.
Save karlseguin/5742394 to your computer and use it in GitHub Desktop.
Testable time.Now in GoLang
package t
import (
"time"
)
type NowFunc func() time.Time
var Now = func() time.Time { return time.Now() }
func NowForce(unix int) {
Now = func() time.Time { return time.Unix(int64(unix), 0) }
}
// use t.Now() throughout your code
// and force a specific timestamp via t.NowForce(1233) in tests
@jackdesert
Copy link

Is this a typo? NowFunc / NowForce

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment