time.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Copyright 2016 Google Inc. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package uuid
  5. import (
  6. "encoding/binary"
  7. "sync"
  8. "time"
  9. )
  10. // A Time represents a time as the number of 100's of nanoseconds since 15 Oct
  11. // 1582.
  12. type Time int64
  13. const (
  14. lillian = 2299160 // Julian day of 15 Oct 1582
  15. unix = 2440587 // Julian day of 1 Jan 1970
  16. epoch = unix - lillian // Days between epochs
  17. g1582 = epoch * 86400 // seconds between epochs
  18. g1582ns100 = g1582 * 10000000 // 100s of a nanoseconds between epochs
  19. )
  20. var (
  21. timeMu sync.Mutex
  22. lasttime uint64 // last time we returned
  23. clockSeq uint16 // clock sequence for this run
  24. timeNow = time.Now // for testing
  25. )
  26. // UnixTime converts t the number of seconds and nanoseconds using the Unix
  27. // epoch of 1 Jan 1970.
  28. func (t Time) UnixTime() (sec, nsec int64) {
  29. sec = int64(t - g1582ns100)
  30. nsec = (sec % 10000000) * 100
  31. sec /= 10000000
  32. return sec, nsec
  33. }
  34. // GetTime returns the current Time (100s of nanoseconds since 15 Oct 1582) and
  35. // clock sequence as well as adjusting the clock sequence as needed. An error
  36. // is returned if the current time cannot be determined.
  37. func GetTime() (Time, uint16, error) {
  38. defer timeMu.Unlock()
  39. timeMu.Lock()
  40. return getTime()
  41. }
  42. func getTime() (Time, uint16, error) {
  43. t := timeNow()
  44. // If we don't have a clock sequence already, set one.
  45. if clockSeq == 0 {
  46. setClockSequence(-1)
  47. }
  48. now := uint64(t.UnixNano()/100) + g1582ns100
  49. // If time has gone backwards with this clock sequence then we
  50. // increment the clock sequence
  51. if now <= lasttime {
  52. clockSeq = ((clockSeq + 1) & 0x3fff) | 0x8000
  53. }
  54. lasttime = now
  55. return Time(now), clockSeq, nil
  56. }
  57. // ClockSequence returns the current clock sequence, generating one if not
  58. // already set. The clock sequence is only used for Version 1 UUIDs.
  59. //
  60. // The uuid package does not use global static storage for the clock sequence or
  61. // the last time a UUID was generated. Unless SetClockSequence is used, a new
  62. // random clock sequence is generated the first time a clock sequence is
  63. // requested by ClockSequence, GetTime, or NewUUID. (section 4.2.1.1)
  64. func ClockSequence() int {
  65. defer timeMu.Unlock()
  66. timeMu.Lock()
  67. return clockSequence()
  68. }
  69. func clockSequence() int {
  70. if clockSeq == 0 {
  71. setClockSequence(-1)
  72. }
  73. return int(clockSeq & 0x3fff)
  74. }
  75. // SetClockSequence sets the clock sequence to the lower 14 bits of seq. Setting to
  76. // -1 causes a new sequence to be generated.
  77. func SetClockSequence(seq int) {
  78. defer timeMu.Unlock()
  79. timeMu.Lock()
  80. setClockSequence(seq)
  81. }
  82. func setClockSequence(seq int) {
  83. if seq == -1 {
  84. var b [2]byte
  85. randomBits(b[:]) // clock sequence
  86. seq = int(b[0])<<8 | int(b[1])
  87. }
  88. oldSeq := clockSeq
  89. clockSeq = uint16(seq&0x3fff) | 0x8000 // Set our variant
  90. if oldSeq != clockSeq {
  91. lasttime = 0
  92. }
  93. }
  94. // Time returns the time in 100s of nanoseconds since 15 Oct 1582 encoded in
  95. // uuid. The time is only defined for version 1, 2, 6 and 7 UUIDs.
  96. func (uuid UUID) Time() Time {
  97. var t Time
  98. switch uuid.Version() {
  99. case 6:
  100. time := binary.BigEndian.Uint64(uuid[:8]) // Ignore uuid[6] version b0110
  101. t = Time(time)
  102. case 7:
  103. time := binary.BigEndian.Uint64(uuid[:8])
  104. t = Time((time>>16)*10000 + g1582ns100)
  105. default: // forward compatible
  106. time := int64(binary.BigEndian.Uint32(uuid[0:4]))
  107. time |= int64(binary.BigEndian.Uint16(uuid[4:6])) << 32
  108. time |= int64(binary.BigEndian.Uint16(uuid[6:8])&0xfff) << 48
  109. t = Time(time)
  110. }
  111. return t
  112. }
  113. // ClockSequence returns the clock sequence encoded in uuid.
  114. // The clock sequence is only well defined for version 1 and 2 UUIDs.
  115. func (uuid UUID) ClockSequence() int {
  116. return int(binary.BigEndian.Uint16(uuid[8:10])) & 0x3fff
  117. }