version7.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright 2023 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. "io"
  7. )
  8. // UUID version 7 features a time-ordered value field derived from the widely
  9. // implemented and well known Unix Epoch timestamp source,
  10. // the number of milliseconds seconds since midnight 1 Jan 1970 UTC, leap seconds excluded.
  11. // As well as improved entropy characteristics over versions 1 or 6.
  12. //
  13. // see https://datatracker.ietf.org/doc/html/draft-peabody-dispatch-new-uuid-format-03#name-uuid-version-7
  14. //
  15. // Implementations SHOULD utilize UUID version 7 over UUID version 1 and 6 if possible.
  16. //
  17. // NewV7 returns a Version 7 UUID based on the current time(Unix Epoch).
  18. // Uses the randomness pool if it was enabled with EnableRandPool.
  19. // On error, NewV7 returns Nil and an error
  20. func NewV7() (UUID, error) {
  21. uuid, err := NewRandom()
  22. if err != nil {
  23. return uuid, err
  24. }
  25. makeV7(uuid[:])
  26. return uuid, nil
  27. }
  28. // NewV7FromReader returns a Version 7 UUID based on the current time(Unix Epoch).
  29. // it use NewRandomFromReader fill random bits.
  30. // On error, NewV7FromReader returns Nil and an error.
  31. func NewV7FromReader(r io.Reader) (UUID, error) {
  32. uuid, err := NewRandomFromReader(r)
  33. if err != nil {
  34. return uuid, err
  35. }
  36. makeV7(uuid[:])
  37. return uuid, nil
  38. }
  39. // makeV7 fill 48 bits time (uuid[0] - uuid[5]), set version b0111 (uuid[6])
  40. // uuid[8] already has the right version number (Variant is 10)
  41. // see function NewV7 and NewV7FromReader
  42. func makeV7(uuid []byte) {
  43. /*
  44. 0 1 2 3
  45. 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  46. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  47. | unix_ts_ms |
  48. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  49. | unix_ts_ms | ver | rand_a (12 bit seq) |
  50. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  51. |var| rand_b |
  52. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  53. | rand_b |
  54. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  55. */
  56. _ = uuid[15] // bounds check
  57. t, s := getV7Time()
  58. uuid[0] = byte(t >> 40)
  59. uuid[1] = byte(t >> 32)
  60. uuid[2] = byte(t >> 24)
  61. uuid[3] = byte(t >> 16)
  62. uuid[4] = byte(t >> 8)
  63. uuid[5] = byte(t)
  64. uuid[6] = 0x70 | (0x0F & byte(s>>8))
  65. uuid[7] = byte(s)
  66. }
  67. // lastV7time is the last time we returned stored as:
  68. //
  69. // 52 bits of time in milliseconds since epoch
  70. // 12 bits of (fractional nanoseconds) >> 8
  71. var lastV7time int64
  72. const nanoPerMilli = 1000000
  73. // getV7Time returns the time in milliseconds and nanoseconds / 256.
  74. // The returned (milli << 12 + seq) is guarenteed to be greater than
  75. // (milli << 12 + seq) returned by any previous call to getV7Time.
  76. func getV7Time() (milli, seq int64) {
  77. timeMu.Lock()
  78. defer timeMu.Unlock()
  79. nano := timeNow().UnixNano()
  80. milli = nano / nanoPerMilli
  81. // Sequence number is between 0 and 3906 (nanoPerMilli>>8)
  82. seq = (nano - milli*nanoPerMilli) >> 8
  83. now := milli<<12 + seq
  84. if now <= lastV7time {
  85. now = lastV7time + 1
  86. milli = now >> 12
  87. seq = now & 0xfff
  88. }
  89. lastV7time = now
  90. return milli, seq
  91. }