Skip to content
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

Merged
merged 1 commit into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion time.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ func (uuid UUID) Time() Time {
var t Time
switch uuid.Version() {
case 6:
time := binary.BigEndian.Uint64(uuid[:8]) // Ignore uuid[6] version b0110
time := int64(binary.BigEndian.Uint32(uuid[0:4])) << 28
time |= int64(binary.BigEndian.Uint16(uuid[4:6])) << 12
time |= int64(binary.BigEndian.Uint16(uuid[6:8]) & 0xfff)
t = Time(time)
case 7:
time := binary.BigEndian.Uint64(uuid[:8])
Expand Down
12 changes: 8 additions & 4 deletions version6.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Collaborator

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

Copy link
Contributor Author

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?

Copy link
Collaborator

@bormanp bormanp Jun 3, 2024

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.

timeLow := uint16(now & 0x0fff)
timeLow |= 0x6000 // Version 6

binary.BigEndian.PutUint32(uuid[0:], timeHigh)
binary.BigEndian.PutUint16(uuid[4:], timeMid)
binary.BigEndian.PutUint16(uuid[6:], timeLow)
binary.BigEndian.PutUint16(uuid[8:], seq)

uuid[6] = 0x60 | (uuid[6] & 0x0F)
uuid[8] = 0x80 | (uuid[8] & 0x3F)

nodeMu.Lock()
if nodeID == zeroID {
setNodeInterface("")
Expand Down
Loading