forked from ProtonMail/gopenpgp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
time.go
44 lines (35 loc) · 970 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
package crypto
import (
"time"
)
var pgp = GopenPGP{}
// GetGopenPGP return global GopenPGP
func GetGopenPGP() *GopenPGP {
return &pgp
}
// UpdateTime updates cached time
func (pgp *GopenPGP) UpdateTime(newTime int64) {
pgp.latestServerTime = newTime
pgp.latestClientTime = time.Now()
}
// GetTimeUnix gets latest cached time
func (pgp *GopenPGP) GetTimeUnix() int64 {
return pgp.getNow().Unix()
}
// GetTime gets latest cached time
func (pgp *GopenPGP) GetTime() time.Time {
return pgp.getNow()
}
func (pgp *GopenPGP) getNow() time.Time {
if pgp.latestServerTime > 0 && !pgp.latestClientTime.IsZero() {
// Until is monotonic, it uses a monotonic clock in this case instead of the wall clock
extrapolate := int64(time.Until(pgp.latestClientTime).Seconds())
return time.Unix(pgp.latestServerTime+extrapolate, 0)
}
return time.Now()
}
func (pgp *GopenPGP) getTimeGenerator() func() time.Time {
return func() time.Time {
return pgp.getNow()
}
}