This repository was archived by the owner on Oct 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclock.go
More file actions
69 lines (55 loc) · 1.28 KB
/
clock.go
File metadata and controls
69 lines (55 loc) · 1.28 KB
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
59
60
61
62
63
64
65
66
67
68
69
package gotime
import "time"
type Clock struct {
baseTime
}
func (c Clock) Hour() int {
return c.t.Hour()
}
func (c Clock) Minute() int {
return c.t.Minute()
}
func (c Clock) Second() int {
return c.t.Second()
}
func (c Clock) Nanosecond() int {
return c.t.Nanosecond()
}
func (c Clock) Location() *time.Location {
return c.t.Location()
}
// Sub is equivalent to .GetTime().Sub()
func (c Clock) Sub(otherC Clock) time.Duration {
return c.t.Sub(otherC.t)
}
func (c Clock) ToDayNano() int64 {
epochInTz := time.Date(1970, 1, 1, 0, 0, 0, 0, c.t.Location())
return c.t.Sub(epochInTz).Nanoseconds()
}
func NewClockFromTime(t time.Time) Clock {
t = t.Local()
hour, minute, second := t.Clock()
nano := t.Nanosecond()
loc := t.Location()
return Clock{
baseTime{
t: time.Date(1, 1, 1, hour, minute, second, nano, loc),
},
}
}
func NewClock(hour, minute, second, nanosecond int, location *time.Location) Clock {
return Clock{
baseTime{
t: time.Date(1970, 1, 1, hour, minute, second, nanosecond, location),
},
}
}
func NewClockOfDayNano(nanoOfDay int64, location *time.Location) Clock {
// todo check for time greater than one day
epochInTz := time.Date(1970, 1, 1, 0, 0, 0, 0, location)
return Clock{
baseTime{
t: epochInTz.Add(time.Duration(nanoOfDay)),
},
}
}