-
Notifications
You must be signed in to change notification settings - Fork 450
/
time.go
124 lines (106 loc) · 3.6 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
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
// Copyright (c) 2016 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// Package time implement facilities for working with time.
package time
import (
"time"
)
const (
nanosPerMillis = int64(time.Millisecond)
)
// Now returns the current local time, in nanoseconds elapsed since January 1,
// 1970 UTC. NB: This is independent of location.
func Now() UnixNano {
return ToUnixNano(time.Now())
}
// Since returns the time elapsed since t.
// It is shorthand for time.Now().Sub(t).
func Since(t UnixNano) time.Duration {
return Now().Sub(t)
}
// ToNormalizedTime returns the normalized units of time given a time unit.
func ToNormalizedTime(t time.Time, u time.Duration) int64 {
return t.UnixNano() / u.Nanoseconds()
}
// FromNormalizedTime returns the time given the normalized time units and the time unit.
func FromNormalizedTime(nt int64, u time.Duration) UnixNano {
return UnixNano(int64(u/time.Nanosecond) * nt)
}
// ToNormalizedDuration returns the normalized units of duration given a time unit.
func ToNormalizedDuration(d time.Duration, u time.Duration) int64 {
return int64(d / u)
}
// FromNormalizedDuration returns the duration given the normalized time duration and a time unit.
func FromNormalizedDuration(nd int64, u time.Duration) time.Duration {
return time.Duration(nd) * u
}
// ToNanoseconds converts a time to nanoseconds.
func ToNanoseconds(t time.Time) int64 {
return t.UnixNano()
}
// FromNanoseconds converts nanoseconds to a time.
func FromNanoseconds(nsecs int64) time.Time {
return time.Unix(0, nsecs)
}
// ToUnixMillis converts a time to milliseconds since Unix epoch
func ToUnixMillis(t time.Time) int64 {
return t.UnixNano() / nanosPerMillis
}
// FromUnixMillis converts milliseconds since Unix epoch to a time
func FromUnixMillis(ms int64) time.Time {
return time.Unix(0, ms*nanosPerMillis)
}
// Ceil returns the result of rounding t up to a multiple of d since
// the zero time.
func Ceil(t time.Time, d time.Duration) time.Time {
res := t.Truncate(d)
if res.Before(t) {
res = res.Add(d)
}
return res
}
// MinTime returns the earlier one of t1 and t2.
func MinTime(t1, t2 time.Time) time.Time {
if t1.Before(t2) {
return t1
}
return t2
}
// MinUnixNano returns the earlier one of t1 and t2.
func MinUnixNano(t1, t2 UnixNano) UnixNano {
if t1.Before(t2) {
return t1
}
return t2
}
// MaxTime returns the later one of t1 and t2.
func MaxTime(t1, t2 time.Time) time.Time {
if t1.After(t2) {
return t1
}
return t2
}
// MaxUnixNano returns the later one of t1 and t2.
func MaxUnixNano(t1, t2 UnixNano) UnixNano {
if t1.After(t2) {
return t1
}
return t2
}