time.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // Copyright 2014 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. clock_seq 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 clock_seq == 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. clock_seq = ((clock_seq + 1) & 0x3fff) | 0x8000
  53. }
  54. lasttime = now
  55. return Time(now), clock_seq, 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 a new random
  62. // clock sequence is generated the first time a clock sequence is requested by
  63. // ClockSequence, GetTime, or NewUUID. (section 4.2.1.1) sequence is generated
  64. // for
  65. func ClockSequence() int {
  66. defer timeMu.Unlock()
  67. timeMu.Lock()
  68. return clockSequence()
  69. }
  70. func clockSequence() int {
  71. if clock_seq == 0 {
  72. setClockSequence(-1)
  73. }
  74. return int(clock_seq & 0x3fff)
  75. }
  76. // SetClockSeq sets the clock sequence to the lower 14 bits of seq. Setting to
  77. // -1 causes a new sequence to be generated.
  78. func SetClockSequence(seq int) {
  79. defer timeMu.Unlock()
  80. timeMu.Lock()
  81. setClockSequence(seq)
  82. }
  83. func setClockSequence(seq int) {
  84. if seq == -1 {
  85. var b [2]byte
  86. randomBits(b[:]) // clock sequence
  87. seq = int(b[0])<<8 | int(b[1])
  88. }
  89. old_seq := clock_seq
  90. clock_seq = uint16(seq&0x3fff) | 0x8000 // Set our variant
  91. if old_seq != clock_seq {
  92. lasttime = 0
  93. }
  94. }
  95. // Time returns the time in 100s of nanoseconds since 15 Oct 1582 encoded in
  96. // uuid. It returns false if uuid is not valid. The time is only well defined
  97. // for version 1 and 2 UUIDs.
  98. func (uuid UUID) Time() (Time, bool) {
  99. if len(uuid) != 16 {
  100. return 0, false
  101. }
  102. time := int64(binary.BigEndian.Uint32(uuid[0:4]))
  103. time |= int64(binary.BigEndian.Uint16(uuid[4:6])) << 32
  104. time |= int64(binary.BigEndian.Uint16(uuid[6:8])&0xfff) << 48
  105. return Time(time), true
  106. }
  107. // ClockSequence returns the clock sequence encoded in uuid. It returns false
  108. // if uuid is not valid. The clock sequence is only well defined for version 1
  109. // and 2 UUIDs.
  110. func (uuid UUID) ClockSequence() (int, bool) {
  111. if len(uuid) != 16 {
  112. return 0, false
  113. }
  114. return int(binary.BigEndian.Uint16(uuid[8:10])) & 0x3fff, true
  115. }