version7.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 |
  50. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  51. |var| rand_b |
  52. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  53. | rand_b |
  54. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  55. */
  56. _ = uuid[15] // bounds check
  57. t := timeNow().UnixMilli()
  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 | (uuid[6] & 0x0F)
  65. // uuid[8] has already has right version
  66. }