Method: FFaker::UUID#uuidv7

Defined in:
lib/ffaker/uuid.rb

#uuidv7Object

> UUID version 7 features a time-ordered value field derived from the > widely implemented and well known Unix Epoch timestamp source, the > number of milliseconds since midnight 1 Jan 1970 UTC, leap seconds > excluded. UUIDv7 generally has improved entropy characteristics over > UUIDv1 Section 5.1 or UUIDv6 Section 5.6.



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/ffaker/uuid.rb', line 100

def uuidv7
  timestamp = rand((2**48) - 1)

  uuid = 0
  # unix_ts_ms
  # > 48 bit big-endian unsigned number of Unix epoch timestamp in
  # > milliseconds as per Section 6.1. Occupies bits 0 through 47 (octets
  # > 0-5).
  uuid |= timestamp << 80
  # ver
  # > The 4 bit version field as defined by Section 4.2, set to 0b0111 (7).
  # > Occupies bits 48 through 51 of octet 6.
  uuid |= 0b0111 << 76
  # rand_a
  # > 12 bits pseudo-random data to provide uniqueness as per Section 6.9
  # > and/or optional constructs to guarantee additional monotonicity as
  # > per Section 6.2. Occupies bits 52 through 63 (octets 6-7).
  uuid |= rand((2**12) - 1) << 64
  # var
  # > The 2 bit variant field as defined by Section 4.1, set to 0b10.
  # > Occupies bits 64 and 65 of octet 8.
  uuid |= 0b10 << 62
  # rand_b
  # > The final 62 bits of pseudo-random data to provide uniqueness as per
  # > Section 6.9 and/or an optional counter to guarantee additional
  # > monotonicity as per Section 6.2. Occupies bits 66 through 127 (octets
  # > 8-15).
  uuid |= rand((2**62) - 1)

  as_string(uuid)
end