time.go 512 B

123456789101112131415161718192021222324252627
  1. package protocol
  2. import (
  3. "time"
  4. "v2ray.com/core/common/dice"
  5. "v2ray.com/core/common/serial"
  6. )
  7. type Timestamp int64
  8. func (v Timestamp) Bytes(b []byte) []byte {
  9. return serial.Int64ToBytes(int64(v), b)
  10. }
  11. type TimestampGenerator func() Timestamp
  12. func NowTime() Timestamp {
  13. return Timestamp(time.Now().Unix())
  14. }
  15. func NewTimestampGenerator(base Timestamp, delta int) TimestampGenerator {
  16. return func() Timestamp {
  17. rangeInDelta := dice.Roll(delta*2) - delta
  18. return base + Timestamp(rangeInDelta)
  19. }
  20. }