passforios-gopenpgp/time.go

33 lines
731 B
Go
Raw Normal View History

2018-09-02 15:02:26 +02:00
package pmcrypto
2018-06-04 16:05:14 -07:00
import (
"time"
)
// UpdateTime update cached time
func (o *OpenPGP) UpdateTime(newTime int64) {
2018-06-04 17:50:26 -07:00
o.latestServerTime = newTime
o.latestClientTime = time.Now()
2018-06-04 16:05:14 -07:00
}
//GetTime get latest cached time
func (o *OpenPGP) GetTime() int64 {
return o.getNow().Unix()
2018-06-04 16:05:14 -07:00
}
func (o *OpenPGP) getNow() time.Time {
if o.latestServerTime > 0 && !o.latestClientTime.IsZero() {
// Sub is monotome, it uses a monotime time clock in this case instead of the wall clock
extrapolate := int64(o.latestClientTime.Sub(time.Now()).Seconds())
return time.Unix(o.latestServerTime + extrapolate, 0)
2018-06-04 16:05:14 -07:00
}
return time.Now()
}
func (o *OpenPGP) getTimeGenerator() func() time.Time {
return func() time.Time {
return o.getNow()
}
}