Skip to content

[repo-assist] feat: add TimeSpan interop — Add(TimeSpan), operator+ and operator-#18

Draft
github-actions[bot] wants to merge 1 commit into
mainfrom
repo-assist/improve-timespan-interop-d000cf6eb259d84e
Draft

[repo-assist] feat: add TimeSpan interop — Add(TimeSpan), operator+ and operator-#18
github-actions[bot] wants to merge 1 commit into
mainfrom
repo-assist/improve-timespan-interop-d000cf6eb259d84e

Conversation

@github-actions

Copy link
Copy Markdown
Contributor

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

Summary

Add natural .NET TimeSpan interoperability to DateTimeNano:

  • DateTimeNano.Add(TimeSpan) — shift a timestamp by a TimeSpan
  • operator +(DateTimeNano, TimeSpan) — shift forward
  • operator -(DateTimeNano, TimeSpan) — shift back

Rationale

System.DateTime supports +/- with TimeSpan out of the box, and many .NET APIs return durations as TimeSpan. Adding the same operators to DateTimeNano makes the two types easier to use together:

Before:

var later = timestamp.AddMilliseconds((long)myTimeSpan.TotalMilliseconds);
var earlier = timestamp.AddNanoseconds(-myTimeSpan.Ticks * 100);

After:

var later   = timestamp + myTimeSpan;
var earlier = timestamp - myTimeSpan;

The operators complement the existing operator -(DateTimeNano, DateTimeNano) → long and the pending operator +(DateTimeNano, long) / operator -(DateTimeNano, long) in PR #15.

Implementation notes

  • Delegates to the existing AddNanoseconds(long) (TimeSpan.Ticks * 100 converts ticks → nanoseconds). Overflow/underflow protection is fully inherited.
  • TimeSpan precision is 100 ns (one tick); sub-tick nanoseconds stored in DateTimeNano are unaffected.
  • No new dependencies. No breaking changes.

Test Status

✅ 44 tests pass on net8.0 (40 original + 4 new tests covering Add(TimeSpan) and both operators for positive and negative durations). Build succeeded for net8.0, net9.0, and net10.0 with 0 warnings.

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

Add natural .NET TimeSpan interoperability to DateTimeNano:

- DateTimeNano.Add(TimeSpan) — shift by a TimeSpan, negative allowed
- operator+(DateTimeNano, TimeSpan) — shift forward
- operator-(DateTimeNano, TimeSpan) — shift back

Implementation delegates to the existing AddNanoseconds(long) method
(TimeSpan.Ticks * 100 = nanoseconds), so overflow/underflow protection
is fully inherited. TimeSpan precision is 100 ns (one tick); sub-tick
nanoseconds on the DateTimeNano side are unaffected by the addition.

This complements operator-(DateTimeNano, DateTimeNano) and mirrors the
ergonomics of System.DateTime (DateTime + TimeSpan, DateTime - TimeSpan),
allowing idiomatic .NET duration arithmetic alongside DataBento-style
nanosecond offsets.

4 new tests added; all 44 tests pass on net8.0.

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