Skip to content
Open
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
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"dotnet.defaultSolution": "NUlid.sln"
}
33 changes: 33 additions & 0 deletions NUlid/DateTimeExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;

namespace NUlid;

/// <summary>
/// Extension methods for <see cref="DateTime"/> and <see cref="DateTimeOffset"/> to create <see cref="Ulid"/>s.
/// </summary>
public static class DateTimeExtensions
{
/// <summary>
/// Returns a new <see cref="Ulid"/> based on the current <see cref="DateTime"/> value.
/// </summary>
/// <param name="dateTimeOffset">date time offset to base new ULID upon</param>
/// <returns>A new <see cref="Ulid"/></returns>
public static Ulid NewUlid(this DateTimeOffset dateTimeOffset)
=> Ulid.NewUlid(dateTimeOffset);

/// <summary>
/// Returns the minimum <see cref="Ulid"/> based on the current <see cref="DateTime"/> value.
/// </summary>
/// <param name="dateTimeOffset">date time offset to base new ULID upon</param>
/// <returns>A new <see cref="Ulid"/></returns>
public static Ulid MinUlid(this DateTimeOffset dateTimeOffset)
=> Ulid.MinAt(dateTimeOffset);

/// <summary>
/// Returns the maximum <see cref="Ulid"/> based on the current <see cref="DateTime"/> value.
/// </summary>
/// <param name="dateTimeOffset">date time offset to base new ULID upon</param>
/// <returns>A new <see cref="Ulid"/></returns>
public static Ulid MaxUlid(this DateTimeOffset dateTimeOffset)
=> Ulid.MaxAt(dateTimeOffset);
}
88 changes: 44 additions & 44 deletions NUlid/NUlid.csproj
Original file line number Diff line number Diff line change
@@ -1,50 +1,50 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;net6.0</TargetFrameworks>
<Authors>RobIII</Authors>
<Company>Devcorner.nl</Company>
<PackageId>NUlid</PackageId>
<NoPackageAnalysis>true</NoPackageAnalysis>
<Product>NUlid</Product>
<Copyright>(C) 2016 - 2025 Devcorner.nl</Copyright>
<Nullable>enable</Nullable>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageProjectUrl>https://github.com/RobThree/NUlid</PackageProjectUrl>
<PackageTags>Universally Unique Lexicographically Sortable Identifier;ULID;UUID;GUID</PackageTags>
<Description>A .Net ULID implementation</Description>
<LangVersion>latest</LangVersion>
<PackageIcon>logo.png</PackageIcon>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<DocumentationFile>bin\Release\nulid.xml</DocumentationFile>
<Configurations>Debug;Release</Configurations>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
<AnalysisLevel>latest</AnalysisLevel>
<PackageReadmeFile>README.md</PackageReadmeFile>
<RepositoryUrl>https://github.com/RobThree/NUlid</RepositoryUrl>
<RepositoryType>git</RepositoryType>
</PropertyGroup>
<PropertyGroup>
<TargetFrameworks>netstandard2.0;net6.0</TargetFrameworks>
<Authors>RobIII</Authors>
<Company>Devcorner.nl</Company>
<PackageId>NUlid</PackageId>
<NoPackageAnalysis>true</NoPackageAnalysis>
<Product>NUlid</Product>
<Copyright>(C) 2016 - 2025 Devcorner.nl</Copyright>
<Nullable>enable</Nullable>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageProjectUrl>https://github.com/RobThree/NUlid</PackageProjectUrl>
<PackageTags>Universally Unique Lexicographically Sortable Identifier;ULID;UUID;GUID</PackageTags>
<Description>A .Net ULID implementation</Description>
<LangVersion>latest</LangVersion>
<PackageIcon>logo.png</PackageIcon>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<DocumentationFile>bin\Release\nulid.xml</DocumentationFile>
<Configurations>Debug;Release</Configurations>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
<AnalysisLevel>latest</AnalysisLevel>
<PackageReadmeFile>README.md</PackageReadmeFile>
<RepositoryUrl>https://github.com/RobThree/NUlid</RepositoryUrl>
<RepositoryType>git</RepositoryType>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<DefineConstants>TRACE;RELEASE</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<DefineConstants>TRACE;RELEASE</DefineConstants>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|net45|AnyCPU'">
<DocumentationFile>bin\release\NUlid.xml</DocumentationFile>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|net45|AnyCPU'">
<DocumentationFile>bin\release\NUlid.xml</DocumentationFile>
</PropertyGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="System.Memory" Version="4.6.3" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="System.Memory" Version="4.6.3" />
</ItemGroup>

<ItemGroup>
<None Include="..\logo.png">
<Pack>True</Pack>
<PackagePath></PackagePath>
</None>
<None Include="..\README.md">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
</ItemGroup>
</Project>
<ItemGroup>
<None Include="..\logo.png">
<Pack>True</Pack>
<PackagePath></PackagePath>
</None>
<None Include="..\README.md">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
</ItemGroup>
</Project>
29 changes: 23 additions & 6 deletions NUlid/Ulid.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using NUlid.Rng;
using System;
using System;
using System.Buffers.Binary;
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
using NUlid.Rng;


#if NET6_0_OR_GREATER
using System.Buffers.Binary;
using System.Runtime.CompilerServices;
using System.Runtime.Intrinsics;
#endif
Expand Down Expand Up @@ -53,12 +54,12 @@ public struct Ulid : IEquatable<Ulid>, IComparable<Ulid>, IComparable, ISerializ
/// <summary>
/// Represents the smallest possible value of <see cref="Ulid"/>. This field is read-only.
/// </summary>
public static readonly Ulid MinValue = new(_epoch, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
public static readonly Ulid MinValue = MinAt(_epoch);

/// <summary>
/// Represents the largest possible value of <see cref="Ulid"/>. This field is read-only.
/// </summary>
public static readonly Ulid MaxValue = new(DateTimeOffset.MaxValue, [255, 255, 255, 255, 255, 255, 255, 255, 255, 255]);
public static readonly Ulid MaxValue = MaxAt(DateTimeOffset.MaxValue);

/// <summary>
/// A read-only instance of the <see cref="Ulid"/> structure whose value is all zeros.
Expand Down Expand Up @@ -104,6 +105,22 @@ public static Ulid NewUlid(DateTimeOffset time)
public static Ulid NewUlid(IUlidRng rng)
=> NewUlid(DateTimeOffset.UtcNow, rng);

/// <summary>
/// Creates and returns a new <see cref="Ulid"/> that is the minimum possible value for the specified time.
/// </summary>
/// <param name="time">The <see cref="DateTimeOffset"/> to use for the time-part of the <see cref="Ulid"/>.</param>
/// <returns>Returns a new <see cref="Ulid"/>.</returns>
public static Ulid MinAt(DateTimeOffset time)
=> new(time, new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 });
Copy link

Copilot AI May 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Consider extracting the fixed byte array [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] into a static readonly field to avoid new allocations on each call.

Suggested change
=> new(time, new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 });
=> new(time, MinBytes);

Copilot uses AI. Check for mistakes.


/// <summary>
/// Creates and returns a new <see cref="Ulid"/> that is the maximum possible value for the specified time.
/// </summary>
/// <param name="time">The <see cref="DateTimeOffset"/> to use for the time-part of the <see cref="Ulid"/>.</param>
/// <returns>Returns a new <see cref="Ulid"/>.</returns>
public static Ulid MaxAt(DateTimeOffset time)
=> new(time, new byte[] { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255 });
Comment on lines +121 to +122
Copy link

Copilot AI May 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Consider extracting the fixed byte array [255, 255, 255, 255, 255, 255, 255, 255, 255, 255] into a static readonly field to avoid new allocations on each call.

Suggested change
public static Ulid MaxAt(DateTimeOffset time)
=> new(time, new byte[] { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255 });
private static readonly byte[] MaxBytes = { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255 };
public static Ulid MaxAt(DateTimeOffset time)
=> new(time, MaxBytes);

Copilot uses AI. Check for mistakes.


/// <summary>
/// Creates and returns a new <see cref="Ulid"/> based on the specified time and using the specified RNG.
/// </summary>
Expand Down Expand Up @@ -868,4 +885,4 @@ public readonly string ToString(string? format, IFormatProvider? formatProvider)
//public static Ulid FromString(string ulid) => new Ulid(ulid);
//public static implicit operator Ulid(string ulid) => FromString(ulid);
//public static implicit operator string(Ulid ulid) => ulid.ToString();
}
}
7 changes: 7 additions & 0 deletions global.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"sdk": {
"version": "9.0.100",
"allowPrerelease": false,
"rollForward": "latestMinor"
}
}