[repo-assist] feat: implement IFormattable, IParsable(T), and ISpanParsable(T) on DateTimeNano#26
Conversation
…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>
|
@copilot resolve the merge conflicts in this pull request |
…arsable-formattable-2026-06-4efd657cb446a3f4 # Conflicts: # DateTimeNano.Tests/DateTimeNanoTests.cs # DateTimeNano/DateTimeNano.cs
Merge conflicts resolved in commit Both files had conflicts between this PR's
All 87 tests pass on net8.0, net9.0, and net10.0. |
There was a problem hiding this comment.
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>, andISpanParsable<DateTimeNano>onDateTimeNano. - 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); |
| static DateTimeNano ISpanParsable<DateTimeNano>.Parse(ReadOnlySpan<char> s, IFormatProvider? provider) | ||
| => Parse(s.ToString()); |
🤖 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:IFormattablestring.Format("{0:yyyy-MM-dd}", nano)and$"{nano:HH:mm:ss}"IParsable<DateTimeNano>where T : IParsable<T>constraintsISpanParsable<DateTimeNano>Why
IFormattableDateTimeNano.ToString()always returns the full 9-digit nanosecond string. WithIFormattable, callers can request anyDateTimeformat string via string interpolation:Format specifiers
null,"G","g","O","o"all return the full nanosecond string. Any other format delegates toDateTime.ToString(format, provider)at DateTime precision (sub-microsecond nanoseconds noted in XML doc).IParsable<DateTimeNano>Allows
DateTimeNanoto satisfy generic constraints likewhere T : IParsable<T>, enabling use with parsing utilities, JSON converters, and .NET's own generic infrastructure introduced in .NET 7.ISpanParsable<DateTimeNano>(extendsIParsable)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
Parse(string, IFormatProvider?)andTryParse(string?, IFormatProvider?, out DateTimeNano)overloads are added as public methods (not explicit-only), so they're discoverable via IDE and usable without casting.ISpanParsablemethods are explicit to avoid overload ambiguity with the existingTryParse(string?, out DateTimeNano).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 outputToString("yyyy-MM-dd", ...)→ date-only outputstring.FormatinterpolationParse(string, IFormatProvider?)overloadTryParse(string?, IFormatProvider?, out)valid and invalid inputswhere T : IParsable<T>ISpanParsable.TryParsevia generic constraint