Skip to content

Instantly share code, notes, and snippets.

@tobias-kuendig
Created November 26, 2019 17:18
Show Gist options
  • Save tobias-kuendig/a9a38e34e4aa6719cbf0561aa66ebd0e to your computer and use it in GitHub Desktop.
Save tobias-kuendig/a9a38e34e4aa6719cbf0561aa66ebd0e to your computer and use it in GitHub Desktop.
Golang Clock Package
package clock
import "time"
// Clock is a wrapper around time functions to make
// time dependent code easily testable.
type Clock struct {
now time.Time
}
// FromTime returns a new clock with a set time.
func FromTime(t time.Time) *Clock {
return &Clock{now: t}
}
// Return the current time.
func (c *Clock) Now() time.Time {
if c == nil {
return time.Now()
}
return c.now
}
// Set the current time.
func (c *Clock) SetNow(t time.Time) {
c.now = t
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment