-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathclock.go
56 lines (47 loc) · 1.21 KB
/
clock.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
package main
import "time"
// Clock keeps track of the current time.
type Clock struct {
t time.Time
isRealTime bool
}
// A new Clock in the local timezone at the current time now.
func NewClockNow() *Clock {
clock := new(Clock)
clock.t = time.Now()
clock.isRealTime = true
return clock
}
// A new Clock with the given time and timezone.
func NewClockTime(t time.Time) *Clock {
clock := new(Clock)
clock.t = t
clock.isRealTime = false
return clock
}
// A new Clock in the local timezone at the `time.Unix` timestamp.
func NewClockUnixTimestamp(sec int64) *Clock {
clock := new(Clock)
clock.t = time.Unix(sec, 0)
clock.isRealTime = false
return clock
}
// AddDays adds n days to the current date.
func (c *Clock) AddDays(n int) {
c.t = c.t.AddDate(0, 0, n)
c.isRealTime = false
}
// AddDays adds n hours to the current date-time, keeping the minutes
func (c *Clock) AddHours(n int) {
c.t = c.t.Add(time.Hour * time.Duration(n))
c.isRealTime = false
}
// AddMinutes adds n minutes to the current date-time, keeping the seconds
func (c *Clock) AddMinutes(n int) {
c.t = c.t.Add(time.Minute * time.Duration(n))
c.isRealTime = false
}
// Get the wrapped time.Time struct
func (c *Clock) Time() time.Time {
return c.t
}