Skip to content

[repo-assist] feat: add + and - arithmetic operators for nanosecond offsets#15

Draft
github-actions[bot] wants to merge 1 commit into
mainfrom
repo-assist/improve-arithmetic-operators-6fb5774105313644
Draft

[repo-assist] feat: add + and - arithmetic operators for nanosecond offsets#15
github-actions[bot] wants to merge 1 commit into
mainfrom
repo-assist/improve-arithmetic-operators-6fb5774105313644

Conversation

@github-actions

Copy link
Copy Markdown
Contributor

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

Summary

Add two new arithmetic operators to DateTimeNano:

  • operator +(DateTimeNano, long) — shift a timestamp forward by N nanoseconds
  • operator -(DateTimeNano, long) — shift a timestamp back by N nanoseconds

These are thin wrappers around the existing, validated AddNanoseconds method, so overflow/underflow protection is fully inherited.

Rationale

The type already exposes operator -(DateTimeNano, DateTimeNano) that returns the nanosecond difference. The natural complement is to also allow timestamp + offset and timestamp - offset arithmetic with a plain long nanosecond count. This matches the ergonomics of System.DateTime (DateTime + TimeSpan, DateTime - TimeSpan).

Before:

var next = current.AddNanoseconds(500);
var prev = current.AddNanoseconds(-500);

After:

var next = current + 500L;
var prev = current - 500L;

Trade-offs

  • operator -(DateTimeNano, long) and operator -(DateTimeNano, DateTimeNano) coexist because their right-hand types are different (long vs DateTimeNano). No ambiguity.
  • No new dependencies introduced.

Test Status

✅ 44 tests pass on net8.0 (40 original + 4 new tests covering the new operators).

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

Add two new operators:
- operator+(DateTimeNano, long): shift a timestamp forward by N nanoseconds
- operator-(DateTimeNano, long): shift a timestamp back by N nanoseconds

These complement the existing operator-(DateTimeNano, DateTimeNano) that
returns the nanosecond difference, and are thin wrappers around the already-
validated AddNanoseconds method (overflow/underflow protection is inherited).

Add four unit tests covering positive offset, negative offset, sign inversion
via the + operator, and underflow guard.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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.

0 participants