Class Uuid7Extensions
Provides comprehensive extension methods for generating and working with UUID v7 strings and GUIDs.
public static class Uuid7Extensions
- Inheritance
-
Uuid7Extensions
- Inherited Members
Remarks
UUID v7 is a time-ordered UUID format defined in RFC 9562. It includes a Unix timestamp with millisecond precision, making it suitable for use as a database primary key with natural time-based ordering.
Built on Medo.Uuid7 v3.x. Version detection and timestamp extraction delegate to the
Medo library to avoid manual byte-layout handling, which changed between v1 and v3
(v3's default ToGuid() returns a mixed-endian Guid that round-trips
via Uuid7.FromGuid but whose raw ToByteArray() layout no longer
places the version nibble at byte index 6).
Methods
CompareByTimestamp(Guid, Guid)
Compares two UUID v7 values by their timestamp.
public static int CompareByTimestamp(Guid uuid1, Guid uuid2)
Parameters
Returns
- int
A value less than 0 if uuid1 is earlier, 0 if they have the same timestamp, or greater than 0 if uuid1 is later.
ExtractTimestamp(Guid)
Extracts the timestamp from a UUID v7 Guid.
public static DateTimeOffset? ExtractTimestamp(Guid uuid)
Parameters
uuidGuidThe UUID v7 Guid.
Returns
- DateTimeOffset?
The timestamp embedded in the UUID, or null if extraction fails or the Guid is not a v7 UUID.
ExtractTimestamp(string)
Extracts the timestamp from a UUID v7 string.
public static DateTimeOffset? ExtractTimestamp(string uuidString)
Parameters
uuidStringstringThe UUID v7 string.
Returns
- DateTimeOffset?
The timestamp embedded in the UUID, or null if extraction fails.
GenerateGuid()
Generates a new UUID v7 as a Guid object.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Guid GenerateGuid()
Returns
GenerateGuids(int)
Generates multiple UUID v7 GUIDs efficiently.
public static Guid[] GenerateGuids(int count)
Parameters
countintThe number of UUIDs to generate.
Returns
- Guid[]
An array of generated GUIDs.
GenerateSequentialGuids(int, int)
Generates a sequential range of UUID v7 GUIDs with millisecond spacing.
public static IEnumerable<Guid> GenerateSequentialGuids(int count, int intervalMs = 1)
Parameters
Returns
- IEnumerable<Guid>
An enumerable of sequential GUIDs.
Remarks
Iterator methods (yield return) cannot be async, so interval pacing uses a short spin-wait.
GenerateString()
Generates a new UUID v7 string in a compact 25-character format.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string GenerateString()
Returns
- string
A compact UUID v7 string representation.
GenerateStrings(int)
Generates multiple UUID v7 strings efficiently.
public static string[] GenerateStrings(int count)
Parameters
countintThe number of UUIDs to generate.
Returns
- string[]
An array of generated UUID strings.
IsValidUuid7Guid(Guid)
Validates whether a GUID is a valid UUID v7.
public static bool IsValidUuid7Guid(Guid guidValue)
Parameters
guidValueGuidThe GUID to validate.
Returns
- bool
trueif the GUID is a valid UUID v7; otherwise,false.
IsValidUuid7String(string?)
Validates whether a string is a valid UUID v7.
public static bool IsValidUuid7String(string? uuidString)
Parameters
uuidStringstringThe string to validate.
Returns
- bool
trueif the string is a valid UUID v7; otherwise,false.
ToGuid(string)
Converts a UUID v7 string to a Guid.
public static Guid ToGuid(string uuidString)
Parameters
uuidStringstringThe UUID string to convert.
Returns
ToUuid7String(Guid)
Converts a GUID to a UUID v7 string.
public static string? ToUuid7String(Guid guidValue)
Parameters
guidValueGuidThe GUID to convert.
Returns
- string
The UUID v7 string, or null if the GUID is empty or not a v7 UUID.