Repository navigation
Add DateOnly and TimeOnly polyfills - #138
Conversation
|
@copilot while the types have been introduced in .NET 6, have you checked whether all members you've polyfilled were also introduced in .NET 6 or maybe some of them were introduced later? |
There was a problem hiding this comment.
Pull request overview
Adds polyfills for the .NET 6 DateOnly and TimeOnly types to PolyShim so consumers can use these APIs on older target frameworks, along with accompanying tests and documentation updates.
Changes:
- Added
System.DateOnlyandSystem.TimeOnlypolyfill implementations underPolyShim/Net60/. - Added Net60 test coverage for the new types.
- Updated README and regenerated
Signatures.mdto include the new polyfilled types.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 8 comments.
Show a summary per file
| File | Description |
|---|---|
| Signatures.md | Updates generated signature/type counts and lists DateOnly/TimeOnly. |
| Readme.md | Documents DateOnly/TimeOnly as available type polyfills. |
| PolyShim/Net60/DateOnly.cs | Introduces DateOnly polyfill implementation. |
| PolyShim/Net60/TimeOnly.cs | Introduces TimeOnly polyfill implementation. |
| PolyShim.Tests/Net60/DateOnlyTests.cs | Adds tests for DateOnly behavior. |
| PolyShim.Tests/Net60/TimeOnlyTests.cs | Adds tests for TimeOnly behavior. |
Suppressed comments (12)
PolyShim/Net60/TimeOnly.cs:259
- TimeOnly.TryParse currently accepts strings that include a date component because it relies on DateTime.TryParse and then drops the date. Add a validation that the parsed date is the default (0001-01-01) produced by NoCurrentDateDefault, otherwise return false.
if (
s is not null
&& DateTime.TryParse(
s,
provider,
PolyShim/Net60/TimeOnly.cs:212
- TimeOnly.ParseExact(string, string, ...) uses DateTime.ParseExact and then drops the date, which can allow date components to sneak in via a format that includes them. Implement ParseExact in terms of TryParseExact so the same date-component validation is applied consistently.
public static TimeOnly ParseExact(
string s,
string format,
IFormatProvider? provider,
DateTimeStyles style = DateTimeStyles.None
PolyShim/Net60/TimeOnly.cs:225
- TimeOnly.ParseExact(string, string[], ...) uses DateTime.ParseExact and then drops the date, which can allow date components to sneak in via a format that includes them. Implement ParseExact in terms of TryParseExact so the same date-component validation is applied consistently.
public static TimeOnly ParseExact(
string s,
string[] formats,
IFormatProvider? provider,
DateTimeStyles style = DateTimeStyles.None
PolyShim/Net60/TimeOnly.cs:301
- TimeOnly.TryParseExact should reject inputs that include a date component. Add a check that the parsed date is 0001-01-01 (the default date produced by NoCurrentDateDefault when only a time is present).
if (
s is not null
&& format is not null
&& DateTime.TryParseExact(
s,
PolyShim/Net60/TimeOnly.cs:336
- TimeOnly.TryParseExact (formats overload) should reject inputs that include a date component. Add a check that the parsed date is 0001-01-01 (the default date produced by NoCurrentDateDefault when only a time is present).
if (
s is not null
&& formats is not null
&& DateTime.TryParseExact(
s,
formats,
PolyShim/Net60/DateOnly.cs:145
- DateOnly.ParseExact uses DateTime.ParseExact and then drops the time. That allows date+time formats/inputs to parse successfully and silently discard the time. Implement ParseExact in terms of TryParseExact so the same time-component validation is applied consistently.
public static DateOnly ParseExact(
string s,
string format,
IFormatProvider? provider,
DateTimeStyles style = DateTimeStyles.None
PolyShim/Net60/DateOnly.cs:155
- DateOnly.ParseExact (formats overload) uses DateTime.ParseExact and then drops the time. That allows date+time formats/inputs to parse successfully and silently discard the time. Implement ParseExact in terms of TryParseExact so the same time-component validation is applied consistently.
public static DateOnly ParseExact(
string s,
string[] formats,
IFormatProvider? provider,
DateTimeStyles style = DateTimeStyles.None
PolyShim/Net60/DateOnly.cs:163
- DateOnly.ParseExact(ReadOnlySpan, ...) currently parses via DateTime.ParseExact directly and bypasses any validation added to the string-based ParseExact/TryParseExact. Delegate span overloads to the string overloads to keep behavior consistent.
public static DateOnly ParseExact(
ReadOnlySpan<char> s,
ReadOnlySpan<char> format,
IFormatProvider? provider = null,
DateTimeStyles style = DateTimeStyles.None
) =>
PolyShim/Net60/DateOnly.cs:178
- DateOnly.ParseExact(ReadOnlySpan, string[] ...) currently parses via DateTime.ParseExact directly and bypasses any validation added to the string-based ParseExact/TryParseExact. Delegate span overloads to the string overloads to keep behavior consistent.
public static DateOnly ParseExact(
ReadOnlySpan<char> s,
string[] formats,
IFormatProvider? provider = null,
DateTimeStyles style = DateTimeStyles.None
) => FromDateTime(DateTime.ParseExact(new string(s.ToArray()), formats, provider, style));
PolyShim/Net60/DateOnly.cs:194
- DateOnly.TryParse should reject inputs that include a time component. Add a check that the parsed time-of-day is exactly 00:00:00.000 (otherwise return false).
if (s is not null && DateTime.TryParse(s, provider, style, out var dateTime))
{
result = FromDateTime(dateTime);
return true;
}
PolyShim/Net60/DateOnly.cs:228
- DateOnly.TryParseExact should reject inputs/formats that include a time component. Add a check that the parsed time-of-day is exactly 00:00:00.000 (otherwise return false).
if (
s is not null
&& format is not null
&& DateTime.TryParseExact(s, format, provider, style, out var dateTime)
)
PolyShim/Net60/DateOnly.cs:256
- DateOnly.TryParseExact (formats overload) should reject inputs/formats that include a time component. Add a check that the parsed time-of-day is exactly 00:00:00.000 (otherwise return false).
if (
s is not null
&& formats is not null
&& DateTime.TryParseExact(s, formats, provider, style, out var dateTime)
)
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Good catch — not all polyfilled members were from .NET 6. Here's what I found and fixed:
These were previously placed in
All other polyfilled members ( The |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
|
@copilot please see other review comments and also fix build errors |
…yfills - Add missing private readonly DateTime _dateTime field to DateOnly struct - Fix DateOnly.TryParse/TryParseExact to reject inputs with time components - Fix DateOnly.Parse/ParseExact to delegate to TryParse/TryParseExact and throw FormatException on failure - Fix TimeOnly.TryParse/TryParseExact to reject inputs with date components - Fix TimeOnly.Parse/ParseExact to delegate to TryParse/TryParseExact and throw FormatException on failure - Remove non-BCL ToDateTime(DateOnly) overloads from TimeOnly (BCL does not expose these) - Add DateOnly test: TryParse rejects input with time component - Add TimeOnly test: TryParse rejects input with date component
Fixed in commit
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## prime #138 +/- ##
==========================================
+ Coverage 99.20% 99.24% +0.03%
==========================================
Files 154 158 +4
Lines 4170 4383 +213
Branches 89 89
==========================================
+ Hits 4137 4350 +213
Misses 13 13
Partials 20 20 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Done — applied all the same consolidations to |
Co-authored-by: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com>
…uctor like DateOnly Co-authored-by: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 10 out of 10 changed files in this pull request and generated no new comments.
Suppressed comments (1)
PolyShim/Net60/TimeOnly.cs:114
FromTimeSpan(TimeSpan)currently accepts values outside the valid time-of-day range (e.g. >= 24h). That can produceTimeOnlyinstances whoseTicks/ToTimeSpan()represent more than a day, which is inconsistent withTimeOnlysemantics (and differs from the BCL API, which throws for out-of-range inputs).
public static TimeOnly FromDateTime(DateTime dateTime) =>
new(new DateTime(dateTime.TimeOfDay.Ticks));
public static TimeOnly FromTimeSpan(TimeSpan timeSpan) => new(new DateTime(timeSpan.Ticks));
DateOnlyandTimeOnly(introduced in .NET 6) had no polyfill, so they were unavailable on older target frameworks.Type polyfills
PolyShim/Net60/DateOnly.csandPolyShim/Net60/TimeOnly.cs, guarded to compile only where the native types are unavailable (older .NET Core, .NET Framework, .NET Standard).DateTime/ticks internally; cover construction, arithmetic (AddDays/AddMonths/AddYears,Add/AddHours/AddMinutes,IsBetween), comparison/equality,Deconstruct,ToString/Parse/TryParse/ParseExact/TryParseExact(string andReadOnlySpan<char>overloads),TryFormat, and conversions to/fromDateTime/TimeSpan.Tests
PolyShim.Tests/Net60/DateOnlyTests.csandTimeOnlyTests.cscovering the new API surface.Docs
Signatures.md.DateOnly/TimeOnlyinReadme.md's type polyfills section.