passforios-gopenpgp/crypto/time.go

44 lines
924 B
Go
Raw Normal View History

package crypto
import (
"time"
)
var pmCrypto = PmCrypto{}
func GetPmCrypto() *PmCrypto {
return &pmCrypto
}
// UpdateTime update cached time
func (pm *PmCrypto) UpdateTime(newTime int64) {
pm.latestServerTime = newTime
pm.latestClientTime = time.Now()
}
//GetTime get latest cached time
func (pm *PmCrypto) GetTimeUnix() int64 {
return pm.getNow().Unix()
}
//GetTime get latest cached time
func (pm *PmCrypto) GetTime() time.Time {
return pm.getNow()
}
func (pm *PmCrypto) getNow() time.Time {
if pm.latestServerTime > 0 && !pm.latestClientTime.IsZero() {
// Sub is monotome, it uses a monotime time clock in this case instead of the wall clock
extrapolate := int64(pm.latestClientTime.Sub(time.Now()).Seconds())
return time.Unix(pm.latestServerTime+extrapolate, 0)
}
return time.Now()
}
func (pm *PmCrypto) getTimeGenerator() func() time.Time {
return func() time.Time {
return pm.getNow()
}
}