2018-09-11 11:09:28 +02:00
|
|
|
package crypto
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
2018-11-05 22:55:45 +01:00
|
|
|
var pmCrypto = PmCrypto{}
|
|
|
|
|
|
2019-05-14 14:42:38 +00:00
|
|
|
// GetPmCrypto return global PmCrypto
|
2018-11-05 22:55:45 +01:00
|
|
|
func GetPmCrypto() *PmCrypto {
|
|
|
|
|
return &pmCrypto
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-13 12:33:01 +00:00
|
|
|
// UpdateTime updates cached time
|
2018-09-11 11:09:28 +02:00
|
|
|
func (pm *PmCrypto) UpdateTime(newTime int64) {
|
|
|
|
|
pm.latestServerTime = newTime
|
|
|
|
|
pm.latestClientTime = time.Now()
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-13 12:33:01 +00:00
|
|
|
// GetTimeUnix gets latest cached time
|
2018-09-20 15:20:45 +02:00
|
|
|
func (pm *PmCrypto) GetTimeUnix() int64 {
|
2018-09-11 11:09:28 +02:00
|
|
|
return pm.getNow().Unix()
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-13 12:33:01 +00:00
|
|
|
// GetTime gets latest cached time
|
2018-09-20 15:20:45 +02:00
|
|
|
func (pm *PmCrypto) GetTime() time.Time {
|
|
|
|
|
return pm.getNow()
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-11 11:09:28 +02:00
|
|
|
func (pm *PmCrypto) getNow() time.Time {
|
|
|
|
|
if pm.latestServerTime > 0 && !pm.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
|
|
|
|
|
extrapolate := int64(time.Until(pm.latestClientTime).Seconds())
|
2018-09-20 15:20:45 +02:00
|
|
|
return time.Unix(pm.latestServerTime+extrapolate, 0)
|
2018-09-11 11:09:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return time.Now()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (pm *PmCrypto) getTimeGenerator() func() time.Time {
|
|
|
|
|
return func() time.Time {
|
|
|
|
|
return pm.getNow()
|
|
|
|
|
}
|
|
|
|
|
}
|