-
Notifications
You must be signed in to change notification settings - Fork 0
/
system.go
43 lines (31 loc) · 1.32 KB
/
system.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
package xtime
import (
"context"
"time"
)
type clock struct{}
func (c *clock) After(d time.Duration) <-chan time.Time { return time.After(d) }
func (c *clock) AfterFunc(d time.Duration, f func()) *MockTimer {
return &MockTimer{timer: time.AfterFunc(d, f)}
}
func (c *clock) Now() time.Time { return time.Now() }
func (c *clock) Unix() int64 { return time.Now().Unix() }
func (c *clock) UnixMilli() int64 { return time.Now().UnixNano() / int64(time.Millisecond) }
func (c *clock) Since(t time.Time) time.Duration { return time.Since(t) }
func (c *clock) Until(t time.Time) time.Duration { return time.Until(t) }
func (c *clock) Sleep(d time.Duration) { time.Sleep(d) }
func (c *clock) Tick(d time.Duration) <-chan time.Time { return time.Tick(d) }
func (c *clock) NewTicker(d time.Duration) *MockTicker {
t := time.NewTicker(d)
return &MockTicker{C: t.C, ticker: t}
}
func (c *clock) Timer(d time.Duration) *MockTimer {
t := time.NewTimer(d)
return &MockTimer{C: t.C, timer: t}
}
func (c *clock) WithDeadline(parent context.Context, d time.Time) (context.Context, context.CancelFunc) {
return context.WithDeadline(parent, d)
}
func (c *clock) WithTimeout(parent context.Context, t time.Duration) (context.Context, context.CancelFunc) {
return context.WithTimeout(parent, t)
}