Skip to content

Commit c3f1f26

Browse files
denizzzkadlang-bot
authored andcommitted
UUIDv7 timestamps: demystifying bitwise magic
1 parent d6af824 commit c3f1f26

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

std/uuid.d

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -568,12 +568,15 @@ public struct UUID
568568
throw new UUIDParsingException("The UUID is not of version" ~
569569
" v7 therefore no timestamp exist", 0);
570570
}
571-
ulong milli = (cast(ulong)(this.data[0]) << 40) |
572-
(cast(ulong)(this.data[1]) << 32) |
573-
(cast(ulong)(this.data[2]) << 24) |
574-
(cast(ulong)(this.data[3]) << 16) |
575-
(cast(ulong)(this.data[4]) << 8) |
576-
(cast(ulong)(this.data[5]));
571+
572+
import std.bitmanip : bigEndianToNative;
573+
574+
ubyte[8] tmp = void;
575+
tmp[0 .. 2] = 0;
576+
tmp[2 .. 8] = data[0 .. 6];
577+
578+
ulong milli = tmp.bigEndianToNative!ulong;
579+
577580
return SysTime(DateTime(1970, 1, 1), UTC()) + dur!"msecs"(milli);
578581
}
579582

0 commit comments

Comments
 (0)