This class correctly implements UUIDv7, following the recommendations of RFC 4122 and the UUIDv7 proposal.
It generates time-sortable UUIDs with the correct version and variant, and with sufficient randomness to guarantee their uniqueness.
Furthermore, it handles collisions within the same millisecond using monotonic increments, following industry-standard practices for modern UUIDs.
48-bit timestamp (time-sortable) - It was corrected
80 random/monotonic bits generated with SecureRandom
Correct version and variant according to RFC 4122
Monotonic: prevents duplicates if multiple UUIDs are generated in the same millisecond
Cross-platform: works on B4A, B4J, and B4i
Standard output: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
It's secure, sortable, reliable, and production-ready.
Note:
If no timestamp is passed (ts=0), the current date/time in milliseconds is used.
If the timestamp is shorter than the previous one, lastTimestamp is used to maintain consistency.
Security and Randomness
* In B4A/B4J, use `SecureRandom` → cryptographic randomness.
* In B4i, use `Rnd` → pseudo-random (less secure, but functional).
Guaranteed Time Monotonicity
* Ensures that UUIDs generated at the same instant are not repeated.
You can test the results of the class on this page that decodes UUIDv7
The following files are attached: class and example.
Regards
It generates time-sortable UUIDs with the correct version and variant, and with sufficient randomness to guarantee their uniqueness.
Furthermore, it handles collisions within the same millisecond using monotonic increments, following industry-standard practices for modern UUIDs.
It's secure, sortable, reliable, and production-ready.
Note:
If no timestamp is passed (ts=0), the current date/time in milliseconds is used.
If the timestamp is shorter than the previous one, lastTimestamp is used to maintain consistency.
Security and Randomness
* In B4A/B4J, use `SecureRandom` → cryptographic randomness.
* In B4i, use `Rnd` → pseudo-random (less secure, but functional).
Guaranteed Time Monotonicity
* Ensures that UUIDs generated at the same instant are not repeated.
B4X:
Dim UUIDv7 As UUIDv7Generator
UUIDv7.Initialize
'1. Generation of UUID lists.
For i = 1 To count
Log(UUIDv7.Generate(0))
Next
'2. Generate a UUID with the current date and time
Log(UUIDv7.Generate(0))
'3. Generate a UUID with the desired date and time.
Dim ts As Long = DateTime.Now
Log(UUIDv7.Generate(ts))
Dim ts As Long = 1773389204833
Log(UUIDv7.Generate(ts))
B4X:
019ce647-ff70-7d5c-aadc-55ae5d41e306
019ce647-ffcd-7b83-ae2d-a0ac2e0a6f88
019ce647-ffcd-7b83-ae2d-a0ac2e0a6f89
019ce647-ffcd-7b83-ae2d-a0ac2e0a6f8a
019ce647-ffce-7008-a5b5-c5a522dbd274
019ce647-ffce-7008-a5b5-c5a522dbd275
019ce647-ffce-7008-a5b5-c5a522dbd276
019ce647-ffce-7008-a5b5-c5a522dbd277
019ce647-ffcf-70f2-8f07-979ad191b094
019ce647-ffcf-70f2-8f07-979ad191b095
019ce647-ffcf-70f2-8f07-979ad191b096
019ce647-ffcf-70f2-8f07-979ad191b097
019ce647-ffd0-7118-ba9f-b0ddf61b4557
019ce647-ffd0-7118-ba9f-b0ddf61b4558
019ce647-ffd0-7118-ba9f-b0ddf61b4559
019ce647-ffd0-7118-ba9f-b0ddf61b455a
019ce647-ffd1-7fd1-a889-4f3b3c022e99
You can test the results of the class on this page that decodes UUIDv7
UUID v7 Parser & Analyzer Tool - Free Online UUID Decoder
Free UUID v7 parser tool to decode timestamps, analyze components, and convert UUIDs instantly.
uuidparser.net
The following files are attached: class and example.
Regards
Attachments
Last edited: