Skip to content

Commit

Permalink
Fixed code style, added comments
Browse files Browse the repository at this point in the history
  • Loading branch information
sven-n committed Aug 5, 2024
1 parent 14070fe commit 5155688
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/Persistence/GuidV7.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <copyright file="GuidHelper.cs" company="MUnique">
// <copyright file="GuidV7.cs" company="MUnique">
// Licensed under the MIT License. See LICENSE file in the project root for full license information.
// </copyright>

Expand All @@ -14,16 +14,24 @@ namespace MUnique.OpenMU.Persistence;
/// </summary>
public static class GuidV7
{
/// <summary>
/// Creates a new random guid.
/// </summary>
/// <returns>The new guid.</returns>
public static Guid NewGuid() => NewGuid(DateTimeOffset.UtcNow);

/// <summary>
/// Creates a new random guid for the specified date.
/// </summary>
/// <returns>The new guid.</returns>
public static Guid NewGuid(DateTimeOffset dateTimeOffset)
{
// We create a buffer which is two bytes bigger than the Guid,
// because we don't need the first two bytes of the timestamp.
Span<byte> buffer = stackalloc byte[18];
var uuidAsBytes = buffer[2..];
var currentTimestamp = dateTimeOffset.ToUnixTimeMilliseconds();
Span<byte> timestampBytes = stackalloc byte[sizeof(long)];

if (!BitConverter.TryWriteBytes(buffer, currentTimestamp))
{
throw new InvalidOperationException("Could not convert the timestamp to bytes.");
Expand Down

0 comments on commit 5155688

Please sign in to comment.