Created
November 26, 2019 17:18
-
-
Save tobias-kuendig/a9a38e34e4aa6719cbf0561aa66ebd0e to your computer and use it in GitHub Desktop.
Golang Clock Package
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 "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