Documentation
¶
Overview ¶
Package clock contains interface and set of implementations for getting current time. For production usage time must be mostly real time. For testing purposes it's often much easier to use mocked time so it will return required time each time.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Clock ¶
type Clock interface {
// Now returns current time. Actual returned time depends on clock implementation
Now() time.Time
}
Clock represents interface which can be used to get current time
func NewMock ¶
NewMock returns Clock which will return passed time each time
Example ¶
package main
import (
"fmt"
"time"
"github.com/anothermemory/clock"
)
func main() {
c := clock.NewMock(time.Date(2017, 11, 24, 17, 0, 0, 0, time.UTC))
fmt.Println(c.Now())
fmt.Println(c.Now())
}
Output: 2017-11-24 17:00:00 +0000 UTC 2017-11-24 17:00:00 +0000 UTC
func NewMockPartial ¶
NewMockPartial returns Clock which will return passed times one by one and then real time each time
Example ¶
package main
import (
"time"
"github.com/anothermemory/clock"
)
func main() {
firstTime := time.Date(2017, 11, 24, 17, 0, 0, 0, time.Local)
secondTime := time.Date(2018, 11, 24, 17, 0, 0, 0, time.Local)
c := clock.NewMockPartial(firstTime, secondTime)
c.Now() // This will return firstTime
c.Now() // This will return secondTime
c.Now() // This will return real time
}
Click to show internal directories.
Click to hide internal directories.