Skip to content
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
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,23 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]


## [v2.6.0] - 2026-11-08
[v2.6.0](https://github.com/TensionDev/UUID/releases/tag/v2.6.0)

### Added
- Added `Uuid.FromGuid(Guid)` to convert from `System.Guid` using its canonical text representation.

### Changed
- Changed `Uuid.ToGuid()` to use the canonical text representation explicitly, preserving the UUID value and variant bits.

### Removed
- Removed `ToMicrosoftVariant()` and `ToRfc4122Variant(Guid)` because they rewrote UUID variant bits and were not needed for `Guid` conversion.

### Fixed
- Fixed UUIDv6 random Node IDs to set the required multicast bit.
- Fixed UUIDv7 Method 1 to seed its counter per timestamp and handle counter exhaustion without wrapping UUID sort order backward.


## [v2.5.1] - 2026-10-10
[v2.5.1](https://github.com/TensionDev/UUID/releases/tag/v2.5.1)

Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ This project references the following documents for implementation.
- Converters:
- `ToGuid()` and `FromGuid(Guid)`
- `ToByteArray()` and `FromBytes(byte[])`
- Variant conversions (`ToVariant1`, `ToVariant2`)
- Parsing utilities:
- `Parse(string)`
- `TryParse(string, out Uuid)`
Expand Down
10 changes: 10 additions & 0 deletions TensionDev.UUID.Tests/UUIDv6Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ public void TestRandomGetNodeID()
Assert.NotEqual(nodeID1, nodeID2);
}

[Fact]
public void TestRandomNodeIDSetsMulticastBit()
{
byte[] nodeID = UUIDv6.GetNodeID();
Uuid uuid = UUIDv6.NewUUIDv6();

Assert.Equal(0x01, nodeID[0] & 0x01);
Assert.Equal(0x01, uuid.ToByteArray()[10] & 0x01);
}

[Fact]
public void TestNewUUIDv6()
{
Expand Down
50 changes: 50 additions & 0 deletions TensionDev.UUID.Tests/UUIDv7Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,56 @@ public void TestNewUUIDv7()
Assert.Equal(expectedUUID, uuid);
}

[Fact]
public void TestMethod1CounterIsMonotonicAndHandlesRollover()
{
DateTime timestamp = DateTime.UtcNow.AddYears(1);
Uuid previous = UUIDv7.NewUUIDv7(timestamp, UUIDv7.GenerationMethod.Method1);
DateTime firstTimestamp = UUIDv7.ToDateTime(previous);
Uuid current = UUIDv7.NewUUIDv7(timestamp, UUIDv7.GenerationMethod.Method1);
Assert.True(previous.CompareTo(current) < 0);

previous = current;
current = UUIDv7.NewUUIDv7(timestamp.AddMilliseconds(1), UUIDv7.GenerationMethod.Method1);
Assert.True(previous.CompareTo(current) < 0);
DateTime advancedTimestamp = UUIDv7.ToDateTime(current);
Assert.Equal(firstTimestamp.AddMilliseconds(1), advancedTimestamp);

// A backwards requested timestamp must continue from the last emitted timestamp.
previous = current;
current = UUIDv7.NewUUIDv7(timestamp, UUIDv7.GenerationMethod.Method1);
Assert.True(previous.CompareTo(current) < 0);
Assert.Equal(advancedTimestamp, UUIDv7.ToDateTime(current));

previous = current;
for (int i = 0; i < 0x1001; ++i)
{
current = UUIDv7.NewUUIDv7(timestamp.AddMilliseconds(1), UUIDv7.GenerationMethod.Method1);
Assert.True(previous.CompareTo(current) < 0);
previous = current;
}

Assert.True(UUIDv7.ToDateTime(current) > firstTimestamp);
}

[Fact]
public void TestNewUUIDv7RejectsDateBeforeUnixEpoch()
{
byte[] randomA = [0x00, 0x00];
byte[] randomB = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00];

Assert.Throws<ArgumentOutOfRangeException>(() =>
UUIDv7.NewUUIDv7(DateTime.UnixEpoch.AddTicks(-1), randomA, randomB));
}

[Fact]
public void TestMethod3Generation()
{
Uuid uuid = UUIDv7.NewUUIDv7(DateTime.UtcNow, UUIDv7.GenerationMethod.Method3);

Assert.True(UUIDv7.IsUUIDv7(uuid));
}

[Fact]
public void TestUUIDVariantField()
{
Expand Down
39 changes: 25 additions & 14 deletions TensionDev.UUID.Tests/UuidTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -339,33 +339,44 @@
}

[Fact]
public void TestToGuid()
public void TestGuidAndUuidUseDifferentByteArrayLayoutsForSameCanonicalValue()
{
Guid expected = new Guid("7d444840-9dc0-11d1-b245-5ffdce74fad2");
Uuid uuid = new Uuid("7d444840-9dc0-11d1-b245-5ffdce74fad2");
// RFC 9562 Appendix A.3 UUIDv4 test vector; UUID octets use network byte order (Section 4).
const string canonical = "919108f7-52d1-4320-9bac-f847db4148a8";
Guid guid = new Guid(canonical);
Uuid uuid = new Uuid(canonical);
byte[] expectedUuidBytes = [0x91, 0x91, 0x08, 0xf7, 0x52, 0xd1, 0x43, 0x20, 0x9b, 0xac, 0xf8, 0x47, 0xdb, 0x41, 0x48, 0xa8];
byte[] expectedGuidBytes = [0xf7, 0x08, 0x91, 0x91, 0xd1, 0x52, 0x20, 0x43, 0x9b, 0xac, 0xf8, 0x47, 0xdb, 0x41, 0x48, 0xa8];

Guid actual = uuid.ToGuid();
Assert.Equal(expected, actual);
Assert.Equal(canonical, uuid.ToString("D"));
Assert.Equal(canonical, guid.ToString("D"));
Assert.Equal(expectedUuidBytes, uuid.ToByteArray());
Assert.Equal(expectedGuidBytes, guid.ToByteArray());
Assert.NotEqual(uuid.ToByteArray(), guid.ToByteArray());
Assert.Equal(guid, uuid.ToGuid());
}

[Fact]
public void TestToVariant2()
public void TestGuidAndUuidInterpretSameByteSequenceDifferently()
{
Guid expected = new Guid("7d444840-9dc0-11d1-d245-5ffdce74fad2");
Uuid uuid = new Uuid("7d444840-9dc0-11d1-b245-5ffdce74fad2");
// The RFC 9562 Appendix A.3 test vector in canonical/network byte order.
byte[] bytes = [0x91, 0x91, 0x08, 0xf7, 0x52, 0xd1, 0x43, 0x20, 0x9b, 0xac, 0xf8, 0x47, 0xdb, 0x41, 0x48, 0xa8];
Uuid uuid = new Uuid(bytes);
Guid guid = new Guid(bytes);

Guid actual = uuid.ToMicrosoftVariant();
Assert.Equal(expected, actual);
Assert.Equal("919108f7-52d1-4320-9bac-f847db4148a8", uuid.ToString("D"));
Assert.Equal("f7089191-d152-2043-9bac-f847db4148a8", guid.ToString("D"));
Assert.NotEqual(uuid.ToString("D"), guid.ToString("D"));
}

[Fact]
public void TestToVariant1()
public void TestFromGuidPreservesValue()
{
Uuid expected = new Uuid("7d444840-9dc0-11d1-9245-5ffdce74fad2");
Guid guid = new Guid("7d444840-9dc0-11d1-d245-5ffdce74fad2");

Uuid actual = Uuid.ToRfc4122Variant(guid);
Assert.Equal(expected, actual);
Uuid actual = Uuid.FromGuid(guid);
Assert.Equal(guid.ToString("D"), actual.ToString("D"));
Assert.Equal(guid, actual.ToGuid());
}

[Fact]
Expand Down Expand Up @@ -450,7 +461,7 @@
string vs = "7d444840-9dc0-11d1-b245-5ffdce74fad2";
Uuid uuid = Uuid.Parse(vs);

bool actualResult = (uuid == other);

Check warning on line 464 in TensionDev.UUID.Tests/UuidTests.cs

View workflow job for this annotation

GitHub Actions / .NET 10.0.x on windows-latest

Possible unintended reference comparison; to get a value comparison, cast the right hand side to type 'Uuid'

Check warning on line 464 in TensionDev.UUID.Tests/UuidTests.cs

View workflow job for this annotation

GitHub Actions / .NET 10.0.x on windows-latest

Possible unintended reference comparison; to get a value comparison, cast the right hand side to type 'Uuid'

Check warning on line 464 in TensionDev.UUID.Tests/UuidTests.cs

View workflow job for this annotation

GitHub Actions / .NET 10.0.x on macos-latest

Possible unintended reference comparison; to get a value comparison, cast the right hand side to type 'Uuid'

Check warning on line 464 in TensionDev.UUID.Tests/UuidTests.cs

View workflow job for this annotation

GitHub Actions / .NET 10.0.x on macos-latest

Possible unintended reference comparison; to get a value comparison, cast the right hand side to type 'Uuid'

Check warning on line 464 in TensionDev.UUID.Tests/UuidTests.cs

View workflow job for this annotation

GitHub Actions / .NET 10.0.x on ubuntu-latest

Possible unintended reference comparison; to get a value comparison, cast the right hand side to type 'Uuid'

Check warning on line 464 in TensionDev.UUID.Tests/UuidTests.cs

View workflow job for this annotation

GitHub Actions / .NET 10.0.x on ubuntu-latest

Possible unintended reference comparison; to get a value comparison, cast the right hand side to type 'Uuid'

Check warning on line 464 in TensionDev.UUID.Tests/UuidTests.cs

View workflow job for this annotation

GitHub Actions / .NET 10.0.x on ubuntu-latest

Possible unintended reference comparison; to get a value comparison, cast the right hand side to type 'Uuid'

Check warning on line 464 in TensionDev.UUID.Tests/UuidTests.cs

View workflow job for this annotation

GitHub Actions / .NET 10.0.x on ubuntu-latest

Possible unintended reference comparison; to get a value comparison, cast the right hand side to type 'Uuid'

Check warning on line 464 in TensionDev.UUID.Tests/UuidTests.cs

View workflow job for this annotation

GitHub Actions / .NET 10.0.x on windows-latest

Possible unintended reference comparison; to get a value comparison, cast the right hand side to type 'Uuid'

Check warning on line 464 in TensionDev.UUID.Tests/UuidTests.cs

View workflow job for this annotation

GitHub Actions / .NET 10.0.x on windows-latest

Possible unintended reference comparison; to get a value comparison, cast the right hand side to type 'Uuid'

Check warning on line 464 in TensionDev.UUID.Tests/UuidTests.cs

View workflow job for this annotation

GitHub Actions / .NET 10.0.x on macos-latest

Possible unintended reference comparison; to get a value comparison, cast the right hand side to type 'Uuid'

Check warning on line 464 in TensionDev.UUID.Tests/UuidTests.cs

View workflow job for this annotation

GitHub Actions / .NET 10.0.x on macos-latest

Possible unintended reference comparison; to get a value comparison, cast the right hand side to type 'Uuid'
Assert.False(actualResult);
}

Expand Down Expand Up @@ -504,7 +515,7 @@
string vs = "7d444840-9dc0-11d1-b245-5ffdce74fad2";
Uuid uuid = Uuid.Parse(vs);

bool actualResult = (uuid != other);

Check warning on line 518 in TensionDev.UUID.Tests/UuidTests.cs

View workflow job for this annotation

GitHub Actions / .NET 10.0.x on windows-latest

Possible unintended reference comparison; to get a value comparison, cast the right hand side to type 'Uuid'

Check warning on line 518 in TensionDev.UUID.Tests/UuidTests.cs

View workflow job for this annotation

GitHub Actions / .NET 10.0.x on windows-latest

Possible unintended reference comparison; to get a value comparison, cast the right hand side to type 'Uuid'

Check warning on line 518 in TensionDev.UUID.Tests/UuidTests.cs

View workflow job for this annotation

GitHub Actions / .NET 10.0.x on macos-latest

Possible unintended reference comparison; to get a value comparison, cast the right hand side to type 'Uuid'

Check warning on line 518 in TensionDev.UUID.Tests/UuidTests.cs

View workflow job for this annotation

GitHub Actions / .NET 10.0.x on macos-latest

Possible unintended reference comparison; to get a value comparison, cast the right hand side to type 'Uuid'

Check warning on line 518 in TensionDev.UUID.Tests/UuidTests.cs

View workflow job for this annotation

GitHub Actions / .NET 10.0.x on ubuntu-latest

Possible unintended reference comparison; to get a value comparison, cast the right hand side to type 'Uuid'

Check warning on line 518 in TensionDev.UUID.Tests/UuidTests.cs

View workflow job for this annotation

GitHub Actions / .NET 10.0.x on ubuntu-latest

Possible unintended reference comparison; to get a value comparison, cast the right hand side to type 'Uuid'

Check warning on line 518 in TensionDev.UUID.Tests/UuidTests.cs

View workflow job for this annotation

GitHub Actions / .NET 10.0.x on ubuntu-latest

Possible unintended reference comparison; to get a value comparison, cast the right hand side to type 'Uuid'

Check warning on line 518 in TensionDev.UUID.Tests/UuidTests.cs

View workflow job for this annotation

GitHub Actions / .NET 10.0.x on ubuntu-latest

Possible unintended reference comparison; to get a value comparison, cast the right hand side to type 'Uuid'

Check warning on line 518 in TensionDev.UUID.Tests/UuidTests.cs

View workflow job for this annotation

GitHub Actions / .NET 10.0.x on windows-latest

Possible unintended reference comparison; to get a value comparison, cast the right hand side to type 'Uuid'

Check warning on line 518 in TensionDev.UUID.Tests/UuidTests.cs

View workflow job for this annotation

GitHub Actions / .NET 10.0.x on windows-latest

Possible unintended reference comparison; to get a value comparison, cast the right hand side to type 'Uuid'

Check warning on line 518 in TensionDev.UUID.Tests/UuidTests.cs

View workflow job for this annotation

GitHub Actions / .NET 10.0.x on macos-latest

Possible unintended reference comparison; to get a value comparison, cast the right hand side to type 'Uuid'

Check warning on line 518 in TensionDev.UUID.Tests/UuidTests.cs

View workflow job for this annotation

GitHub Actions / .NET 10.0.x on macos-latest

Possible unintended reference comparison; to get a value comparison, cast the right hand side to type 'Uuid'
Assert.True(actualResult);
}

Expand Down
2 changes: 1 addition & 1 deletion TensionDev.UUID/TensionDev.UUID.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<PackageId>TensionDev.UUID</PackageId>
<Version>2.5.1</Version>
<Version>2.6.0</Version>
<Authors>TensionDev amsga</Authors>
<Company>TensionDev</Company>
<Product>TensionDev.UUID</Product>
Expand Down
2 changes: 2 additions & 0 deletions TensionDev.UUID/UUIDv6.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
/// <summary>
/// Class Library to generate Universally Unique Identifier (UUID) / Globally Unique Identifier (GUID) based on Version 6 (date-time).
/// </summary>
public static class UUIDv6

Check warning on line 24 in TensionDev.UUID/UUIDv6.cs

View workflow job for this annotation

GitHub Actions / .NET 10.0.x

Rename class 'UUIDv6' to match pascal case naming rules, consider using 'UuiDv6'.

Check warning on line 24 in TensionDev.UUID/UUIDv6.cs

View workflow job for this annotation

GitHub Actions / .NET 10.0.x

Rename class 'UUIDv6' to match pascal case naming rules, consider using 'UuiDv6'.
{
private static Int32 s_clock = Int32.MinValue;
private static readonly DateTime s_epoch = new DateTime(1582, 10, 15, 0, 0, 0, DateTimeKind.Utc);
Expand Down Expand Up @@ -132,12 +132,14 @@
using var randomNumberGenerator = System.Security.Cryptography.RandomNumberGenerator.Create();
Byte[] fakeNode = new Byte[6];
randomNumberGenerator.GetBytes(fakeNode);
fakeNode[0] = (Byte)(fakeNode[0] | 0x01);
return fakeNode;
#else
using (System.Security.Cryptography.RNGCryptoServiceProvider cryptoServiceProvider = new System.Security.Cryptography.RNGCryptoServiceProvider())
{
Byte[] fakeNode = new Byte[6];
cryptoServiceProvider.GetBytes(fakeNode);
fakeNode[0] = (Byte)(fakeNode[0] | 0x01);
return fakeNode;
}
#endif
Expand Down
107 changes: 78 additions & 29 deletions TensionDev.UUID/UUIDv7.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
/// <summary>
/// Class Library to generate Universally Unique Identifier (UUID) / Globally Unique Identifier (GUID) based on Version 7 (date-time).
/// </summary>
public static class UUIDv7

Check warning on line 25 in TensionDev.UUID/UUIDv7.cs

View workflow job for this annotation

GitHub Actions / .NET 10.0.x

Rename class 'UUIDv7' to match pascal case naming rules, consider using 'UuiDv7'.

Check warning on line 25 in TensionDev.UUID/UUIDv7.cs

View workflow job for this annotation

GitHub Actions / .NET 10.0.x

Rename class 'UUIDv7' to match pascal case naming rules, consider using 'UuiDv7'.
{
#if NETCOREAPP2_1_OR_GREATER
private static readonly DateTime s_epoch = DateTime.UnixEpoch;
Expand All @@ -31,6 +31,8 @@
#endif

private static UInt16 s_counter = 0;
private static Int64 s_method1TimestampMilliseconds = -1;
private static UInt16 s_method1Counter;

#if NET9_0_OR_GREATER
private static readonly System.Threading.Lock s_counterLock = new System.Threading.Lock();
Expand Down Expand Up @@ -86,7 +88,9 @@
return NewUUIDv7(dateTime, GetRandomA(), GetRandomB());

case GenerationMethod.Method1:
return NewUUIDv7(dateTime, GetFixedBitLengthDedicatedCounterA(), GetRandomB());
Int64 requestedTimestamp = GetUnixTimestampMilliseconds(dateTime);
Int64 effectiveTimestamp = GetMethod1Counter(requestedTimestamp, out Byte[] counterA);
return NewUUIDv7(effectiveTimestamp, counterA, GetRandomB());

case GenerationMethod.Method3:
return NewUUIDv7(dateTime, GetIncreasedClockPrecisionA(dateTime), GetRandomB());
Expand Down Expand Up @@ -116,20 +120,22 @@
if (randomB.Length < 8)
throw new ArgumentException(String.Format("rand_b contains less than 64-bit: {0} bytes", randomB.Length), nameof(randomB));

TimeSpan timeSince = dateTime.ToUniversalTime() - s_epoch.ToUniversalTime();
Int64 timeInterval = ((Int64)timeSince.TotalMilliseconds) << 16;
Int64 timeInterval = GetUnixTimestampMilliseconds(dateTime);

Byte[] time = BitConverter.GetBytes(System.Net.IPAddress.HostToNetworkOrder(timeInterval));
return NewUUIDv7(timeInterval, randomA, randomB);
}

Byte[] hex = new Byte[16];
private static Uuid NewUUIDv7(Int64 timeInterval, Byte[] randomA, Byte[] randomB)
{

hex[0] = time[0];
hex[1] = time[1];
hex[2] = time[2];
hex[3] = time[3];
Byte[] hex = new Byte[16];

hex[4] = time[4];
hex[5] = time[5];
hex[0] = (Byte)(timeInterval >> 40);
hex[1] = (Byte)(timeInterval >> 32);
hex[2] = (Byte)(timeInterval >> 24);
hex[3] = (Byte)(timeInterval >> 16);
hex[4] = (Byte)(timeInterval >> 8);
hex[5] = (Byte)timeInterval;

hex[6] = (Byte)((randomA[0] & 0x0F) + 0x70);
hex[7] = randomA[1];
Expand Down Expand Up @@ -171,12 +177,14 @@
}

/// <summary>
/// Initialises the 12-bit rand_a based on Method 1 in Section 6.2 and returns it.<br />
/// Returns a Fixed-Length Dedicated Counter 16-bit rand_a.
/// Returns the next value from a standalone 12-bit counter helper.<br />
/// UUIDv7 Method 1 generation uses a separate timestamp-aware counter with rollover handling.
/// </summary>
/// <returns>A byte-array representing the 16-bit rand_a</returns>
public static Byte[] GetFixedBitLengthDedicatedCounterA()
{
// Retained as a low-level wrapping 12-bit counter helper for compatibility.
// UUIDv7 Method1 uses a timestamp-aware counter with rollover handling below.
lock (s_counterLock)
{
Int16 value = Convert.ToInt16(s_counter);
Expand Down Expand Up @@ -253,28 +261,69 @@
throw new ArgumentException(String.Format("{0} is not a Version 7 UUID.", uuid), nameof(uuid));

long timeInterval = GetTimeInterval(uuid);
TimeSpan timeSpan = TimeSpan.FromMilliseconds(timeInterval);

return s_epoch.ToUniversalTime() + timeSpan;
return s_epoch.AddTicks(timeInterval * TimeSpan.TicksPerMillisecond);
}

private static Int64 GetTimeInterval(Uuid uuid)
{
Byte[] hex = uuid.ToByteArray();
Byte[] time = new Byte[8];

time[0] = hex[0];
time[1] = hex[1];
time[2] = hex[2];
time[3] = hex[3];
time[4] = hex[4];
time[5] = hex[5];
time[6] = hex[6];
time[7] = hex[7];

Int64 timeInterval = System.Net.IPAddress.NetworkToHostOrder(BitConverter.ToInt64(time, 0));
timeInterval >>= 16;

return ((Int64)hex[0] << 40)
| ((Int64)hex[1] << 32)
| ((Int64)hex[2] << 24)
| ((Int64)hex[3] << 16)
| ((Int64)hex[4] << 8)
| hex[5];
}

private static Int64 GetUnixTimestampMilliseconds(DateTime dateTime)
{
TimeSpan timeSince = dateTime.ToUniversalTime() - s_epoch;
if (timeSince.Ticks < 0)
throw new ArgumentOutOfRangeException(nameof(dateTime), "The date-time is outside the UUIDv7 48-bit Unix timestamp range.");

Int64 timeInterval = timeSince.Ticks / TimeSpan.TicksPerMillisecond;

if (timeInterval < 0 || timeInterval > 0xFFFFFFFFFFFFL)
throw new ArgumentOutOfRangeException(nameof(dateTime), "The date-time is outside the UUIDv7 48-bit Unix timestamp range.");

return timeInterval;
}

private static Int64 GetMethod1Counter(Int64 requestedTimestamp, out Byte[] counterA)
{
lock (s_counterLock)
{
if (s_method1TimestampMilliseconds < 0 || requestedTimestamp > s_method1TimestampMilliseconds)
{
s_method1TimestampMilliseconds = requestedTimestamp;
s_method1Counter = GetRandomCounterA();
}
else if (s_method1Counter < 0x0FFF)
{
++s_method1Counter;
}
else
{
// Advance the logical timestamp rather than wrapping the counter and sorting backward.
++s_method1TimestampMilliseconds;
s_method1Counter = GetRandomCounterA();
}

counterA = new Byte[]
{
(Byte)(s_method1Counter >> 8),
(Byte)s_method1Counter
};

return s_method1TimestampMilliseconds;
}
}

private static UInt16 GetRandomCounterA()
{
Byte[] randomA = GetRandomA();
return (UInt16)(((randomA[0] & 0x0F) << 8) | randomA[1]);
}
}
}
32 changes: 5 additions & 27 deletions TensionDev.UUID/Uuid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
/// <exception cref="System.ArgumentNullException">s is null</exception>
/// <exception cref="System.FormatException">The format of s is invalid</exception>
/// <exception cref="System.OverflowException">The format of s is invalid</exception>
public Uuid(string s) : this()

Check warning on line 95 in TensionDev.UUID/Uuid.cs

View workflow job for this annotation

GitHub Actions / .NET 10.0.x

Refactor this constructor to reduce its Cognitive Complexity from 16 to the 15 allowed.

Check warning on line 95 in TensionDev.UUID/Uuid.cs

View workflow job for this annotation

GitHub Actions / .NET 10.0.x

Refactor this constructor to reduce its Cognitive Complexity from 16 to the 15 allowed.
{
if (String.IsNullOrEmpty(s))
throw new ArgumentNullException(nameof(s), INVALID_FORMAT_STRING);
Expand Down Expand Up @@ -190,7 +190,7 @@
/// <param name="i">The next byte of the Uuid.</param>
/// <param name="j">The next byte of the Uuid.</param>
/// <param name="k">The next byte of the Uuid.</param>
public Uuid(uint a, ushort b, ushort c, byte d, byte e, byte f, byte g, byte h, byte i, byte j, byte k) : this()

Check warning on line 193 in TensionDev.UUID/Uuid.cs

View workflow job for this annotation

GitHub Actions / .NET 10.0.x

Constructor has 11 parameters, which is greater than the 7 authorized.
{
_time_low = a;
_time_mid = b;
Expand Down Expand Up @@ -361,39 +361,17 @@
/// <returns>A System.Guid object.</returns>
public Guid ToGuid()
{
return new Guid(ToString());
return new Guid(ToString("D"));
}

/// <summary>
/// Return the Microsoft Variant version in System.Guid.
/// </summary>
/// <returns>A System.Guid object.</returns>
public Guid ToMicrosoftVariant()
{
byte newClockSeq = (byte)(_clock_seq_hi_and_reserved & 0x1F);
newClockSeq = (byte)(newClockSeq | 0xC0);
byte[] array = ToByteArray();
array[8] = newClockSeq;
Uuid variant2 = new Uuid(array);

return variant2.ToGuid();
}

/// <summary>
/// Returns the RFC 4122 Variant version in TensionDev.UUID.Uuid.
/// Creates a Uuid from the canonical text representation of a System.Guid.
/// </summary>
/// <param name="guid">The System.Guid object to convert.</param>
/// <returns>A TensionDev.UUID.Uuid object.</returns>
public static Uuid ToRfc4122Variant(Guid guid)
/// <returns>A Uuid object with the same value as the specified Guid.</returns>
public static Uuid FromGuid(Guid guid)
{
Uuid variant2 = new Uuid(guid.ToString());
byte newClockSeq = (byte)(variant2._clock_seq_hi_and_reserved & 0x3F);
newClockSeq = (byte)(newClockSeq | 0x80);
byte[] array = variant2.ToByteArray();
array[8] = newClockSeq;
Uuid variant1 = new Uuid(array);

return variant1;
return new Uuid(guid.ToString("D"));
}

/// <summary>
Expand Down
Loading