Skip to content

Instantly share code, notes, and snippets.

@pboothe
Created January 2, 2025 12:42
Show Gist options
  • Save pboothe/8aede4f00ac342c68adbac5164de80bc to your computer and use it in GitHub Desktop.
Save pboothe/8aede4f00ac342c68adbac5164de80bc to your computer and use it in GitHub Desktop.
A clock for injecting deterministic times during unit tests and real times in production.
package timex
import "time"
type Clock interface {
Now() time.Time
Sleep(d time.Duration)
}
type SystemClock struct{}
func (s *SystemClock) Now() time.Time { return time.Now() }
func (s *SystemClock) Sleep(d time.Duration) { time.Sleep(d) }
type StaticClock struct {
Time time.Time
}
func (s *StaticClock) Now() time.Time { return s.Time }
func (s *StaticClock) Sleep(d time.Duration) { s.Time = s.Time.Add(d) }
@pboothe
Copy link
Author

pboothe commented Jan 2, 2025

I wrote this code and I assign it to the public domain. Use it in good health!

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