2018-09-11 11:09:28 +02:00
|
|
|
package crypto
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
2019-05-14 18:05:01 +02:00
|
|
|
var pgp = GopenPGP{}
|
2018-11-05 22:55:45 +01:00
|
|
|
|
2019-05-14 18:05:01 +02:00
|
|
|
// GetGopenPGP return global GopenPGP
|
|
|
|
|
func GetGopenPGP() *GopenPGP {
|
|
|
|
|
return &pgp
|
2018-11-05 22:55:45 +01:00
|
|
|
}
|
|
|
|
|
|
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()
|
2018-09-11 11:09:28 +02:00
|
|
|
}
|
|
|
|
|
|
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()
|
2018-09-11 11:09:28 +02:00
|
|
|
}
|
|
|
|
|
|
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()
|
2018-09-20 15:20:45 +02:00
|
|
|
}
|
|
|
|
|
|
2019-05-14 18:05:01 +02:00
|
|
|
func (pgp *GopenPGP) getNow() time.Time {
|
|
|
|
|
if pgp.latestServerTime > 0 && !pgp.latestClientTime.IsZero() {
|
2019-05-14 14:42:38 +00:00
|
|
|
// 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)
|
2018-09-11 11:09:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return time.Now()
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-14 18:05:01 +02:00
|
|
|
func (pgp *GopenPGP) getTimeGenerator() func() time.Time {
|
2018-09-11 11:09:28 +02:00
|
|
|
return func() time.Time {
|
2019-05-14 18:05:01 +02:00
|
|
|
return pgp.getNow()
|
2018-09-11 11:09:28 +02:00
|
|
|
}
|
|
|
|
|
}
|