Skip to content

[repo-assist] feat: add DateTimeOffset interop — constructor, ToDateTimeOffsetUtc(), and implicit operators#25

Merged
NichUK merged 2 commits into
mainfrom
repo-assist/improve-datetimeoffset-interop-6c77794e8fe464ea
Jun 17, 2026
Merged

[repo-assist] feat: add DateTimeOffset interop — constructor, ToDateTimeOffsetUtc(), and implicit operators#25
NichUK merged 2 commits into
mainfrom
repo-assist/improve-datetimeoffset-interop-6c77794e8fe464ea

Conversation

@github-actions

Copy link
Copy Markdown
Contributor

🤖 This pull request was created by Repo Assist, an automated AI assistant.

Summary

Add natural DateTimeOffset interoperability to DateTimeNano, complementing the existing DateTime support:

Addition Description
DateTimeNano(DateTimeOffset) Construct from any DateTimeOffset; normalises to UTC
ToDateTimeOffsetUtc() Returns the UTC instant as DateTimeOffset with zero offset
implicit operator DateTimeOffset(DateTimeNano) Implicit conversion to UTC DateTimeOffset
implicit operator DateTimeNano(DateTimeOffset) Implicit conversion from any DateTimeOffset

Why

DateTimeOffset is 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 through DateTime:

Before:

DateTimeOffset dto = new DateTimeOffset(nano.ToDateTimeUtc(), TimeSpan.Zero);
DateTimeNano nano = new DateTimeNano(dto.UtcDateTime);

After:

DateTimeOffset dto = nano;          // implicit
DateTimeNano nano = dto;            // implicit
var dto2 = nano.ToDateTimeOffsetUtc(); // explicit

This mirrors the existing DateTime implicit operators already on the type.

Implementation notes

  • All conversions always normalise to UTC; the stored offset is discarded (consistent with how DateTimeNano represents time as a UTC nanosecond epoch value).
  • Sub-microsecond nanoseconds are truncated in the resulting DateTimeOffset (same limitation as DateTime); they are still accessible via Nanoseconds.
  • No new dependencies. No breaking changes.

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.

Generated by 🌈 Repo Assist, see workflow run. Learn more.

…, 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>
@NichUK

NichUK commented Jun 17, 2026

Copy link
Copy Markdown
Owner

@copilot resolve the merge conflicts in this pull request

…atetimeoffset-interop-6c77794e8fe464ea

# Conflicts:
#	DateTimeNano.Tests/DateTimeNanoTests.cs

Copilot AI commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

@copilot resolve the merge conflicts in this pull request

Merge conflicts resolved in commit 5cdaf20. The conflict in DateTimeNanoTests.cs was between this PR's DateTimeOffset interop tests and new tests merged into main (Parts constructor, Date property, overflow guards, implicit operators, TryParse, Subtract overflow). Both sets of tests are now included — all 75 tests pass across net8.0, net9.0, and net10.0.

Copilot AI requested a review from NichUK June 17, 2026 14:11
@NichUK NichUK marked this pull request as ready for review June 17, 2026 14:13
Copilot AI review requested due to automatic review settings June 17, 2026 14:13
@NichUK NichUK merged commit ab5ebe1 into main Jun 17, 2026
1 check passed
@NichUK NichUK deleted the repo-assist/improve-datetimeoffset-interop-6c77794e8fe464ea branch June 17, 2026 14:14

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 via UtcDateTime.
  • Added ToDateTimeOffsetUtc() plus implicit operators to/from DateTimeOffset.
  • 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.

Comment on lines +456 to +458
/// 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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants