-
Notifications
You must be signed in to change notification settings - Fork 84
/
time.go
58 lines (46 loc) · 1.28 KB
/
time.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
54
55
56
57
58
package gofakes3
import "time"
type TimeSource interface {
Now() time.Time
Since(time.Time) time.Duration
}
type TimeSourceAdvancer interface {
TimeSource
Advance(by time.Duration)
}
// FixedTimeSource provides a source of time that always returns the
// specified time.
func FixedTimeSource(at time.Time) TimeSourceAdvancer {
return &fixedTimeSource{time: at}
}
func DefaultTimeSource() TimeSource {
return &locatedTimeSource{
// XXX: uses time.FixedZone to 'fake' the GMT timezone that S3 uses
// (which is basically just UTC with a different name) to avoid
// time.LoadLocation, which requires zoneinfo.zip to be available and
// can break spectacularly on Windows (https://github.com/golang/go/issues/21881)
// or Docker.
timeLocation: time.FixedZone("GMT", 0),
}
}
type locatedTimeSource struct {
timeLocation *time.Location
}
func (l *locatedTimeSource) Now() time.Time {
return time.Now().In(l.timeLocation)
}
func (l *locatedTimeSource) Since(t time.Time) time.Duration {
return time.Since(t)
}
type fixedTimeSource struct {
time time.Time
}
func (l *fixedTimeSource) Now() time.Time {
return l.time
}
func (l *fixedTimeSource) Since(t time.Time) time.Duration {
return l.time.Sub(t)
}
func (l *fixedTimeSource) Advance(by time.Duration) {
l.time = l.time.Add(by)
}