-
Notifications
You must be signed in to change notification settings - Fork 371
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix incorrect timestamp in uuid v6 #161
Conversation
Thank you for noticing this. While your fix appears correct your bit layout diagram was a bit confusing in were you placed your V6 layout as in the standard:
Figure 1: UUIDv6 Field and Bit Layout A V6 diagram that is less confusing (the author of the patch would probably have gotten it correct with this one):
I will note this is not compatible with v1 and should not be (which was the point of v6). V1 looks like:
Again, this probably should have been written as:
|
@@ -39,12 +39,16 @@ func NewV6() (UUID, error) { | |||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |||
*/ | |||
|
|||
binary.BigEndian.PutUint64(uuid[0:], uint64(now)) | |||
timeHigh := uint32((now >> 28) & 0xffffffff) | |||
timeMid := uint16((now >> 12) & 0xffff) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The & 0xffffffff
and & 0xffff
are not needed as typecasting to uint32 and uint16 automatically do that but hopefully the compiler will figure that out and elide the extra operation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's right. bitmasking is borrowed from the existing uuid v1 code for consistent code style.
Would it be better to remove that part?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hah! I wrote the V1 code back in 2011 when Go was pretty new then and I was being paranoid. Probably would do many things different if I were to rewrite it (which I do not plan on doing).
You can choose if you want to leave it in or take it out. I am okay with either.
Thanks for the review.
The diagram I drew is about timestamp slicing. |
The timestamp part of the uuid v6 implementation is incorrect.
The only lower 60 bits of the timestamp from
GetTime
should be used,but current implementation put entire 64-bit timestamp and overwrite the lower 4-bit part with the version ID.
Therefore, it is not field-compatible with version 1.
Also, we don't need to add variant field at
uuid[8]
, as it is already filled bygetTime
.