Change time global handling (#29)

* Reverse time

* Change time handling global

* Remove debug functions

* Remove *pgp methods
This commit is contained in:
wussler 2019-10-22 18:44:45 +02:00 committed by GitHub
parent d398098113
commit 136c0a5495
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
23 changed files with 154 additions and 138 deletions

View file

@ -2,47 +2,50 @@ package crypto
import (
"time"
"errors"
)
var pgp = GopenPGP{}
// GetGopenPGP return global GopenPGP
func GetGopenPGP() *GopenPGP {
return &pgp
}
// UpdateTime updates cached time
func (pgp *GopenPGP) UpdateTime(newTime int64) {
func UpdateTime(newTime int64) {
pgp.latestServerTime = newTime
pgp.latestClientTime = time.Now()
}
// GetUnixTime gets latest cached time
func (pgp *GopenPGP) GetUnixTime() int64 {
return pgp.getNow().Unix()
func GetUnixTime() int64 {
return getNow().Unix()
}
// GetTime gets latest cached time
func (pgp *GopenPGP) GetTime() time.Time {
return pgp.getNow()
func GetTime() time.Time {
return getNow()
}
// ----- INTERNAL FUNCTIONS -----
// getNow returns current time
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
extrapolate := int64(time.Until(pgp.latestClientTime).Seconds())
return time.Unix(pgp.latestServerTime+extrapolate, 0)
func getNow() time.Time {
extrapolate, err := getDiff()
if err != nil {
return time.Now()
}
return time.Now()
return time.Unix(pgp.latestServerTime + extrapolate, 0)
}
func getDiff() (int64, error) {
if pgp.latestServerTime > 0 && !pgp.latestClientTime.IsZero() {
// Since is monotonic, it uses a monotonic clock in this case instead of the wall clock
return int64(time.Since(pgp.latestClientTime).Seconds()), nil
}
return 0, errors.New("Latest server time not available")
}
// getTimeGenerator Returns a time generator function
func (pgp *GopenPGP) getTimeGenerator() func() time.Time {
func getTimeGenerator() func() time.Time {
return func() time.Time {
return pgp.getNow()
return getNow()
}
}