Skip to content

[repo-assist] feat: implement IFormattable, IParsable(T), and ISpanParsable(T) on DateTimeNano#26

Merged
NichUK merged 2 commits into
mainfrom
repo-assist/forward-parsable-formattable-2026-06-4efd657cb446a3f4
Jun 17, 2026
Merged

[repo-assist] feat: implement IFormattable, IParsable(T), and ISpanParsable(T) on DateTimeNano#26
NichUK merged 2 commits into
mainfrom
repo-assist/forward-parsable-formattable-2026-06-4efd657cb446a3f4

Conversation

@github-actions

Copy link
Copy Markdown
Contributor

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

Summary

Implements three .NET 7/8 generic interfaces on DateTimeNano, making it a first-class citizen of the BCL type system:

Interface What it enables
IFormattable string.Format("{0:yyyy-MM-dd}", nano) and $"{nano:HH:mm:ss}"
IParsable<DateTimeNano> Generic where T : IParsable<T> constraints
ISpanParsable<DateTimeNano> Zero-alloc-compatible span-based parsing

Why

IFormattable

DateTimeNano.ToString() always returns the full 9-digit nanosecond string. With IFormattable, callers can request any DateTime format string via string interpolation:

// Before: manual DateTime extract
var s = nano.ToDateTimeUtc().ToString("yyyy-MM-dd HH:mm:ss");

// After: natural interpolation
var s = $"{nano:yyyy-MM-dd HH:mm:ss}";

Format specifiers null, "G", "g", "O", "o" all return the full nanosecond string. Any other format delegates to DateTime.ToString(format, provider) at DateTime precision (sub-microsecond nanoseconds noted in XML doc).

IParsable<DateTimeNano>

Allows DateTimeNano to satisfy generic constraints like where T : IParsable<T>, enabling use with parsing utilities, JSON converters, and .NET's own generic infrastructure introduced in .NET 7.

ISpanParsable<DateTimeNano> (extends IParsable)

Provides ReadOnlySpan<char> overloads, enabling zero-alloc-compatible parsing pipelines where the input comes from a span (e.g. parsing from a memory-mapped file, network buffer, etc.).

Implementation notes

  • All new interfaces are additive — no existing public API changed.
  • The Parse(string, IFormatProvider?) and TryParse(string?, IFormatProvider?, out DateTimeNano) overloads are added as public methods (not explicit-only), so they're discoverable via IDE and usable without casting.
  • ISpanParsable methods are explicit to avoid overload ambiguity with the existing TryParse(string?, out DateTimeNano).
  • No new dependencies. The struct does not need to become partial (unlike the [GeneratedRegex] approach in PR [repo-assist] perf: replace compiled Regex with [GeneratedRegex] source-generated regex #17).

Test Status

52 tests pass on net8.0, net9.0, and net10.0 (40 original + 12 new tests). New tests cover:

  • ToString(null/G/g/O/o, ...) → full nanosecond output
  • ToString("yyyy-MM-dd", ...) → date-only output
  • string.Format interpolation
  • Parse(string, IFormatProvider?) overload
  • TryParse(string?, IFormatProvider?, out) valid and invalid inputs
  • Generic constraint round-trip via where T : IParsable<T>
  • ISpanParsable.TryParse via generic constraint

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

…ateTimeNano

Adds three .NET 8+ generic interfaces that make DateTimeNano a first-class
citizen in the BCL type system:

- IFormattable: enables string.Format / interpolation with format specifiers
  (e.g. "{0:yyyy-MM-dd}"). Null / G / g / O / o produce full nanosecond output;
  any other DateTime format string delegates to DateTime.ToString at DateTime precision.
- IParsable<DateTimeNano>: exposes Parse(string, IFormatProvider?) and
  TryParse(string?, IFormatProvider?, out DateTimeNano) so DateTimeNano can be
  used in generic contexts constrained to IParsable<T>.
- ISpanParsable<DateTimeNano> (extends IParsable): adds ReadOnlySpan<char>
  overloads for zero-alloc-compatible parsing pipelines.

12 new tests added (52 total, was 40).

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

…arsable-formattable-2026-06-4efd657cb446a3f4

# Conflicts:
#	DateTimeNano.Tests/DateTimeNanoTests.cs
#	DateTimeNano/DateTimeNano.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 7bd6966.

Both files had conflicts between this PR's IFormattable/IParsable/ISpanParsable additions and the DateTimeOffset interop changes merged into main (PR #25):

  • DateTimeNano.cs: Combined partial struct (from main) with the three new interfaces, and kept both the new interface method implementations and the DateTimeOffset constructor/operators.
  • DateTimeNanoTests.cs: Kept all test sections from both sides — IFormattable, IParsable, ISpanParsable tests plus the DateTimeOffset interop, T-separator parse, and overflow tests.

All 87 tests pass on net8.0, net9.0, and net10.0.

Copilot AI requested a review from NichUK June 17, 2026 14:22
@NichUK NichUK marked this pull request as ready for review June 17, 2026 14:23
Copilot AI review requested due to automatic review settings June 17, 2026 14:23
@NichUK NichUK merged commit d15b6f0 into main Jun 17, 2026
4 checks passed
@NichUK NichUK deleted the repo-assist/forward-parsable-formattable-2026-06-4efd657cb446a3f4 branch June 17, 2026 14:24

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 .NET generic formatting/parsing interface support to Seerstone.DateTimeNano so it can participate in BCL patterns such as composite formatting, generic parsing constraints, and span-based parsing APIs.

Changes:

  • Implemented IFormattable, IParsable<DateTimeNano>, and ISpanParsable<DateTimeNano> on DateTimeNano.
  • Added format-based ToString(string?, IFormatProvider?) and parsing overloads required by the interfaces.
  • Added NUnit tests covering formatting, parsing overloads, and generic constraint usage.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
DateTimeNano/DateTimeNano.cs Adds interface implementations and new formatting/parsing entry points.
DateTimeNano.Tests/DateTimeNanoTests.cs Adds tests for the new formatting/parsing behaviors and generic constraint scenarios.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

/// <summary>
/// Parses a <see cref="DateTimeNano"/> from a string, ignoring <paramref name="provider"/>.
/// </summary>
public static DateTimeNano Parse(string s, IFormatProvider? provider) => Parse(s);
Comment on lines +481 to +482
static DateTimeNano ISpanParsable<DateTimeNano>.Parse(ReadOnlySpan<char> s, IFormatProvider? provider)
=> Parse(s.ToString());
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