-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime_of_day.go
159 lines (135 loc) · 4.24 KB
/
time_of_day.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package timex
import (
"errors"
"time"
)
// TimeOfDay represents a time of day.
type TimeOfDay struct {
n int64
}
// NewTimeOfDay returns the time of day corresponding to hour, minute, second, and nanosecond.
func NewTimeOfDay(hour, min, sec, nsec int) (TimeOfDay, error) {
if hour < 0 || hour > 23 {
return TimeOfDay{}, errors.New("hour is out of range [0,23]")
}
if min < 0 || min > 59 {
return TimeOfDay{}, errors.New("minute is out of range [0,59]")
}
if sec < 0 || sec > 59 {
return TimeOfDay{}, errors.New("second is out of range [0,59]")
}
if nsec < 0 || nsec >= 1e9 {
return TimeOfDay{}, errors.New("nanosecond is out of range [0,1e9)")
}
return TimeOfDay{n: timeToNanoseconds(hour, min, sec, nsec)}, nil
}
// MustNewTimeOfDay is like NewTimeOfDay but panics if the time of day cannot be created.
func MustNewTimeOfDay(hour, min, sec, nsec int) TimeOfDay {
timeOfDay, err := NewTimeOfDay(hour, min, sec, nsec)
if err != nil {
panic(`timex: NewTimeOfDay: ` + err.Error())
}
return timeOfDay
}
// TimeOfDayFromTime returns the time of day specified by t.
func TimeOfDayFromTime(t time.Time) TimeOfDay {
hour, min, sec := t.Clock()
nsec := t.Nanosecond()
return TimeOfDay{n: timeToNanoseconds(hour, min, sec, nsec)}
}
// TimeOfDayNow returns the current time of day in the given location.
func TimeOfDayNow(location *time.Location) TimeOfDay {
t := time.Now().In(location)
return TimeOfDayFromTime(t)
}
// Clock returns the hour, minute, second and nanosecond specified by t.
func (t TimeOfDay) Clock() (hour, min, sec, nsec int) {
return nanosecondsToTime(t.n)
}
// Hour returns the hour specified by t.
func (t TimeOfDay) Hour() int {
return int(t.n / nsecsEveryHour)
}
// Minute returns the minute specified by t.
func (t TimeOfDay) Minute() int {
return int(t.n%nsecsEveryHour) / nsecsEveryMinute
}
// Second returns the second specified by t.
func (t TimeOfDay) Second() int {
return int(t.n%nsecsEveryMinute) / nsecsEverySecond
}
// Nanosecond returns the nanosecond specified by t.
func (t TimeOfDay) Nanosecond() int {
return int(t.n % nsecsEverySecond)
}
// norm0 normalize the hi and lo into [0, base].
func norm0(hi, lo, base int64) (int64, int64) {
if lo < 0 {
n := (-lo-1)/base + 1
lo += n * base
hi -= n
}
if lo >= base {
n := lo / base
lo -= n * base
hi += n
}
return hi, lo
}
// Add returns the exceeded days and the time of day corresponding to adding the given number of hours, minutes, seconds and nanoseconds to t.
func (t TimeOfDay) Add(hours, mins, secs, nsecs int) (int, TimeOfDay) {
n := t.n +
int64(hours)*nsecsEveryHour +
int64(mins)*nsecsEveryMinute +
int64(secs)*nsecsEverySecond +
int64(nsecs)
day, n := norm0(0, n, nsecsEveryDay)
return int(day), TimeOfDay{n: n}
}
// AddDuration returns the exceeded days and the time of day corresponding to adding the given time.Duration to t.
func (t TimeOfDay) AddDuration(d time.Duration) (int, TimeOfDay) {
n := t.n + d.Nanoseconds()
day, n := norm0(0, n, nsecsEveryDay)
return int(day), TimeOfDay{n: n}
}
// Sub returns the duration t-tt.
func (t TimeOfDay) Sub(tt TimeOfDay) time.Duration {
return time.Duration(t.n - tt.n)
}
// IsZero reports whether the time of day t is the zero value, 00:00:00.
func (t TimeOfDay) IsZero() bool {
return t.n == 0
}
// Before reports whether the time of day t is before tt.
func (t TimeOfDay) Before(tt TimeOfDay) bool {
return t.n < tt.n
}
// After reports whether the time of day t is after tt.
func (t TimeOfDay) After(tt TimeOfDay) bool {
return t.n > tt.n
}
// Equal reports whether the time of day t and tt is the same time.
func (t TimeOfDay) Equal(tt TimeOfDay) bool {
return t.n == tt.n
}
const (
nsecsEverySecond = 1e9
nsecsEveryMinute = 60 * nsecsEverySecond
nsecsEveryHour = 60 * nsecsEveryMinute
nsecsEveryDay = 24 * nsecsEveryHour
)
func timeToNanoseconds(hour, min, sec, nsec int) int64 {
return int64(hour)*nsecsEveryHour +
int64(min)*nsecsEveryMinute +
int64(sec)*nsecsEverySecond +
int64(nsec)
}
func nanosecondsToTime(nsecs int64) (int, int, int, int) {
hour := nsecs / nsecsEveryHour
nsecs -= hour * nsecsEveryHour
min := nsecs / nsecsEveryMinute
nsecs -= min * nsecsEveryMinute
sec := nsecs / nsecsEverySecond
nsecs -= sec * nsecsEverySecond
return int(hour), int(min), int(sec), int(nsecs)
}