-
Notifications
You must be signed in to change notification settings - Fork 11
/
faketime.go
53 lines (45 loc) · 863 Bytes
/
faketime.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package faketime
import (
"time"
"bou.ke/monkey"
)
type faketime struct {
nowPatch *monkey.PatchGuard
year int
month time.Month
day int
hour int
min int
sec int
nsec int
loc *time.Location
time time.Time
}
func NewFaketime(year int, month time.Month, day, hour, min, sec, nsec int, loc *time.Location) *faketime {
return &faketime{
year: year,
month: month,
day: day,
hour: hour,
min: min,
sec: sec,
nsec: nsec,
loc: loc,
}
}
func NewFaketimeWithTime(t time.Time) *faketime {
return &faketime{
time: t,
}
}
func (f *faketime) Do() {
f.nowPatch = monkey.Patch(time.Now, func() time.Time {
if f.time.IsZero() {
return time.Date(f.year, f.month, f.day, f.hour, f.min, f.sec, f.nsec, f.loc)
}
return f.time
})
}
func (f *faketime) Undo() {
f.nowPatch.Unpatch()
}