[repo-assist] feat: add DateTimeOffset interop — constructor, ToDateTimeOffsetUtc(), and implicit operators#25
Conversation
…, and implicit operators DateTimeOffset is widely used in modern .NET (ASP.NET Core, EF Core, System.Text.Json) and its absence from DateTimeNano means users must convert manually before passing to APIs that require it. Changes: - DateTimeNano(DateTimeOffset): constructs from any offset; converts to UTC before computing NanosecondsSinceEpoch. - ToDateTimeOffsetUtc(): returns the UTC instant as DateTimeOffset with zero offset. Sub-microsecond nanoseconds are noted in the XML doc as truncated. - implicit operator DateTimeOffset(DateTimeNano): lossless for DateTime-precision timestamps; nanosecond-only sub-microsecond part noted. - implicit operator DateTimeNano(DateTimeOffset): always normalises to UTC, mirroring the existing DateTime implicit operator behaviour. 6 new tests added (46 total, was 40). All pass on net8.0, net9.0, net10.0. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
@copilot resolve the merge conflicts in this pull request |
…atetimeoffset-interop-6c77794e8fe464ea # Conflicts: # DateTimeNano.Tests/DateTimeNanoTests.cs
Merge conflicts resolved in commit |
There was a problem hiding this comment.
Pull request overview
Adds DateTimeOffset interoperability to Seerstone.DateTimeNano, aligning it with the existing DateTime conversions while ensuring all conversions normalize to UTC.
Changes:
- Added
DateTimeNano(DateTimeOffset)constructor that normalizes to UTC viaUtcDateTime. - Added
ToDateTimeOffsetUtc()plus implicit operators to/fromDateTimeOffset. - Added NUnit tests covering constructor behavior, UTC normalization, implicit operators, and round-trips.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| DateTimeNano/DateTimeNano.cs | Adds DateTimeOffset constructor, ToDateTimeOffsetUtc(), and implicit conversion operators. |
| DateTimeNano.Tests/DateTimeNanoTests.cs | Adds test coverage for DateTimeOffset interop and UTC normalization behavior. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| /// Returns the UTC date and time as a <see cref="System.DateTimeOffset"/> with a zero UTC offset. | ||
| /// Sub-microsecond nanoseconds are not representable in <see cref="System.DateTimeOffset"/> | ||
| /// and are truncated; access them via <see cref="Nanoseconds"/>. |
| } | ||
|
|
||
| [Test] | ||
| public void ImplicitOperator_NonUtcDateTimeOffset_ShouldNormalisedToUtc() |
🤖 This pull request was created by Repo Assist, an automated AI assistant.
Summary
Add natural
DateTimeOffsetinteroperability toDateTimeNano, complementing the existingDateTimesupport:DateTimeNano(DateTimeOffset)DateTimeOffset; normalises to UTCToDateTimeOffsetUtc()DateTimeOffsetwith zero offsetimplicit operator DateTimeOffset(DateTimeNano)DateTimeOffsetimplicit operator DateTimeNano(DateTimeOffset)DateTimeOffsetWhy
DateTimeOffsetis the preferred timestamp type in modern .NET (ASP.NET Core, EF Core,System.Text.Json,HttpContext, etc.). Without this interop, users must either write manual boilerplate or convert throughDateTime:Before:
After:
This mirrors the existing
DateTimeimplicit operators already on the type.Implementation notes
DateTimeNanorepresents time as a UTC nanosecond epoch value).DateTimeOffset(same limitation asDateTime); they are still accessible viaNanoseconds.Test Status
✅ 46 tests pass on net8.0, net9.0, and net10.0 (40 original + 6 new tests covering the constructor,
ToDateTimeOffsetUtc, both implicit operators, UTC normalisation, and round-trip correctness). Build succeeded with 0 errors.