2018-09-11 11:09:28 +02:00
|
|
|
package crypto
|
|
|
|
|
|
|
|
|
|
import (
|
2019-10-22 18:44:45 +02:00
|
|
|
"errors"
|
2019-12-27 19:35:43 +01:00
|
|
|
"time"
|
2018-09-11 11:09:28 +02:00
|
|
|
)
|
|
|
|
|
|
2020-04-28 13:55:36 +02:00
|
|
|
// UpdateTime updates cached time.
|
2019-10-22 18:44:45 +02:00
|
|
|
func UpdateTime(newTime int64) {
|
2020-07-21 13:42:41 +02:00
|
|
|
if newTime > pgp.latestServerTime {
|
|
|
|
|
pgp.latestServerTime = newTime
|
|
|
|
|
pgp.latestClientTime = time.Now()
|
|
|
|
|
}
|
2018-09-11 11:09:28 +02:00
|
|
|
}
|
|
|
|
|
|
2020-04-28 13:55:36 +02:00
|
|
|
// GetUnixTime gets latest cached time.
|
2019-10-22 18:44:45 +02:00
|
|
|
func GetUnixTime() int64 {
|
|
|
|
|
return getNow().Unix()
|
2018-09-11 11:09:28 +02:00
|
|
|
}
|
|
|
|
|
|
2020-04-28 13:55:36 +02:00
|
|
|
// GetTime gets latest cached time.
|
2019-10-22 18:44:45 +02:00
|
|
|
func GetTime() time.Time {
|
|
|
|
|
return getNow()
|
2018-09-20 15:20:45 +02:00
|
|
|
}
|
|
|
|
|
|
2019-06-03 17:00:01 +02:00
|
|
|
// ----- INTERNAL FUNCTIONS -----
|
|
|
|
|
|
2020-04-28 13:55:36 +02:00
|
|
|
// getNow returns current time.
|
2019-10-22 18:44:45 +02:00
|
|
|
func getNow() time.Time {
|
|
|
|
|
extrapolate, err := getDiff()
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return time.Now()
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-27 19:35:43 +01:00
|
|
|
return time.Unix(pgp.latestServerTime+extrapolate, 0)
|
2019-10-22 18:44:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getDiff() (int64, error) {
|
2019-05-14 18:05:01 +02:00
|
|
|
if pgp.latestServerTime > 0 && !pgp.latestClientTime.IsZero() {
|
2019-10-22 18:44:45 +02:00
|
|
|
// Since is monotonic, it uses a monotonic clock in this case instead of the wall clock
|
|
|
|
|
return int64(time.Since(pgp.latestClientTime).Seconds()), nil
|
2018-09-11 11:09:28 +02:00
|
|
|
}
|
|
|
|
|
|
2019-12-27 19:35:43 +01:00
|
|
|
return 0, errors.New("gopenpgp: latest server time not available")
|
2018-09-11 11:09:28 +02:00
|
|
|
}
|
|
|
|
|
|
2020-04-28 13:55:36 +02:00
|
|
|
// getTimeGenerator Returns a time generator function.
|
2019-10-22 18:44:45 +02:00
|
|
|
func getTimeGenerator() func() time.Time {
|
2019-12-27 19:35:43 +01:00
|
|
|
return getNow
|
2018-09-11 11:09:28 +02:00
|
|
|
}
|