@@ -27,7 +27,7 @@ internal static class GuidHelpers
2727 /// all generated GUIDs are strictly ordered by creation time, regardless of timestamp.
2828 /// </para>
2929 /// </remarks>
30- public static unsafe Guid CreateMonotonicUuid ( DateTimeOffset timestamp )
30+ public static Guid CreateMonotonicUuid ( DateTimeOffset timestamp )
3131 {
3232 // UUID v7 format (RFC 9562):
3333 // - 48 bits: Unix timestamp in milliseconds (big-endian)
@@ -42,15 +42,18 @@ public static unsafe Guid CreateMonotonicUuid(DateTimeOffset timestamp)
4242 // Start with a random GUID and twiddle the relevant bits
4343 Guid guid = Guid . NewGuid ( ) ;
4444
45- int * guidAsInts = ( int * ) & guid ;
46- short * guidAsShorts = ( short * ) & guid ;
47- byte * guidAsBytes = ( byte * ) & guid ;
48-
49- // Set timestamp (48 bits) and version/counter using little-endian layout
50- guidAsInts [ 0 ] = ( int ) ( timestampMs >> 8 ) ;
51- guidAsShorts [ 2 ] = ( short ) ( ( timestampMs & 0xFF ) | ( ( timestampMs >> 40 ) << 8 ) ) ;
52- guidAsShorts [ 3 ] = ( short ) ( ( counter & 0xFFF ) | 0x7000 ) ;
53- guidAsBytes [ 8 ] = ( byte ) ( ( guidAsBytes [ 8 ] & 0x3F ) | 0x80 ) ;
45+ unsafe
46+ {
47+ int * guidAsInts = ( int * ) & guid ;
48+ short * guidAsShorts = ( short * ) & guid ;
49+ byte * guidAsBytes = ( byte * ) & guid ;
50+
51+ // Set timestamp (48 bits) and version/counter using little-endian layout
52+ guidAsInts [ 0 ] = ( int ) ( timestampMs >> 8 ) ;
53+ guidAsShorts [ 2 ] = ( short ) ( ( timestampMs & 0xFF ) | ( ( timestampMs >> 40 ) << 8 ) ) ;
54+ guidAsShorts [ 3 ] = ( short ) ( ( counter & 0xFFF ) | 0x7000 ) ;
55+ guidAsBytes [ 8 ] = ( byte ) ( ( guidAsBytes [ 8 ] & 0x3F ) | 0x80 ) ;
56+ }
5457
5558 return guid ;
5659 }
0 commit comments