passforios-gopenpgp/crypto/time.go

45 lines
970 B
Go
Raw Normal View History

package crypto
import (
"time"
)
2019-05-14 18:05:01 +02:00
var pgp = GopenPGP{}
2019-05-14 18:05:01 +02:00
// GetGopenPGP return global GopenPGP
func GetGopenPGP() *GopenPGP {
return &pgp
}
2019-05-13 12:33:01 +00:00
// UpdateTime updates cached time
2019-05-14 18:05:01 +02:00
func (pgp *GopenPGP) UpdateTime(newTime int64) {
pgp.latestServerTime = newTime
pgp.latestClientTime = time.Now()
}
2019-05-13 12:33:01 +00:00
// GetTimeUnix gets latest cached time
2019-05-14 18:05:01 +02:00
func (pgp *GopenPGP) GetTimeUnix() int64 {
return pgp.getNow().Unix()
}
2019-05-13 12:33:01 +00:00
// GetTime gets latest cached time
2019-05-14 18:05:01 +02:00
func (pgp *GopenPGP) GetTime() time.Time {
return pgp.getNow()
}
2019-05-14 18:05:01 +02:00
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
2019-05-14 18:05:01 +02:00
extrapolate := int64(time.Until(pgp.latestClientTime).Seconds())
return time.Unix(pgp.latestServerTime+extrapolate, 0)
}
return time.Now()
}
2019-05-14 18:05:01 +02:00
func (pgp *GopenPGP) getTimeGenerator() func() time.Time {
return func() time.Time {
2019-05-14 18:05:01 +02:00
return pgp.getNow()
}
}