forked from gravitational/teleport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
time.go
46 lines (40 loc) · 736 Bytes
/
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
package utils
import (
"time"
"github.com/jonboulle/clockwork"
)
// MinTTL finds min non 0 TTL duration,
// if both durations are 0, fails
func MinTTL(a, b time.Duration) time.Duration {
if a == 0 {
return b
}
if b == 0 {
return a
}
if a < b {
return a
}
return b
}
// ToTTL converts expiration time to TTL duration
// relative to current time as provided by clock
func ToTTL(c clockwork.Clock, tm time.Time) time.Duration {
now := c.Now().UTC()
if tm.IsZero() || tm.Before(now) {
return 0
}
return tm.Sub(now)
}
// UTC converts time to UTC timezone
func UTC(t *time.Time) {
if t == nil {
return
}
if t.IsZero() {
// to fix issue with timezones for tests
*t = time.Time{}
return
}
*t = t.UTC()
}