Introduce DateOnly and TimeOnly types#50980
Conversation
|
Note regarding the This serves as a reminder for when your PR is modifying a ref *.cs file and adding/modifying public APIs, to please make sure the API implementation in the src *.cs file is documented with triple slash comments, so the PR reviewers can sign off that change. |
|
CC @safern |
src/libraries/System.Private.CoreLib/src/Resources/Strings.resx
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/Globalization/DateTimeFormat.cs
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/Globalization/DateTimeFormat.cs
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/Resources/Strings.resx
Outdated
Show resolved
Hide resolved
gewarren
left a comment
There was a problem hiding this comment.
There is a lot of /// to review - here is some initial feedback.
src/libraries/System.Private.CoreLib/src/System/Globalization/DateTimeFormat.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/Globalization/DateTimeFormat.cs
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/Globalization/DateTimeFormat.cs
Show resolved
Hide resolved
Added a temorary logging info to the exception for CI failure diagnostics
src/libraries/System.Private.CoreLib/src/Resources/Strings.resx
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/Resources/Strings.resx
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/Resources/Strings.resx
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/Globalization/DateTimeFormat.cs
Show resolved
Hide resolved
|
Thanks @stephentoub & @danmoseley for all valuable feedback. I believe I have addressed all of it. Please let me know if you have any more comments or we are good to go. |
|
I have no other comments but I did not review it all so I defer to @stephentoub |
Co-authored-by: Santiago Fernandez Madero <safern@microsoft.com>
|
@safern thanks for the feedback. do you have any more comments? |
| ParseFailure, | ||
| WrongParts, | ||
| BadFormatSpecifier | ||
| } |
There was a problem hiding this comment.
We can't share the same enum that's used with DateTime?
Also, indentation is off here. And if we can't share it, it should probably be a nested enum under DateOnly rather than being System.ParseOperationResult.
There was a problem hiding this comment.
I tried to avoid sharing that with DateTimeParse.ParseFailureKind enum because they include different things specific to DateTime parsing and I wanted to avoid merging more values there that are not related to DateTime. I didn't make this enum nested inside DateOnly because I used it in TimeOnly too. we can still move it and reference it with the DateOnly but I thought this will be ugly.
For the rest of the comments, I'll try to address this in other PR later.
There was a problem hiding this comment.
I wanted to avoid merging more values there that are not related to DateTime
I think it's fine to add more. DateTime, DateTimeOffset, DateOnly, and TimeOnly are all logically related and part of the same system, sharing many similar errors, share format and styles, etc.
|
|
||
| return new DateOnly(newDayNumber); | ||
|
|
||
| static void ThrowOutOfRange() => throw new ArgumentOutOfRangeException(nameof(value), SR.ArgumentOutOfRange_AddValue); |
There was a problem hiding this comment.
This can't be on ThrowHelper rather than its own method?
| default: | ||
| Debug.Assert(result == ParseOperationResult.WrongParts); | ||
| throw new FormatException(SR.Format(SR.Format_DateTimeOnlyContainsNoneDateParts, s.ToString(), nameof(DateOnly))); | ||
| } |
There was a problem hiding this comment.
This block of exception handling code is duplicated in multiple places. It should instead be a helper method, just as with DateTime. That not only saves on duplication, it makes the call site more streamlined, with rarely used code separated out, makes it more likely to be inlineable, etc.
| /// <returns>true if the s parameter was converted successfully; otherwise, false.</returns> | ||
| public static bool TryParse(string s, IFormatProvider? provider, DateTimeStyles style, out DateOnly result) | ||
| { | ||
| result = default; |
There was a problem hiding this comment.
This result = default; should be moved into the if block below.
| bool b = DateTimeFormat.TryFormatDateOnlyO(value.Year, value.Month, value.Day, destination); | ||
| Debug.Assert(b); | ||
| }); | ||
| } |
There was a problem hiding this comment.
Nit: these braces shouldn't be necessary... same for the 'r' case below
| ParseOperationResult result = TryParseInternal(s, provider, style, out TimeOnly timeOnly); | ||
| if (result != ParseOperationResult.Success) | ||
| { | ||
| switch (result) |
There was a problem hiding this comment.
Same comment as on DateOnly
| { | ||
| case 'o': | ||
| case 'O': | ||
| { |
|
https://github.com/FastFinTech/FFT.TimeStamps I pasted this link in case anyone's interested in code I wrote for my personal workaround for some of the the issues solved by this PR, plus a few more. It has a The main emphasis of this library has a couple of features that are not addressed by this PR:
Whilst Thanks for reading! @mattjohnsonpint, updated to help out with the confused face :) Thanks to my .net heros for your amazing work! |
Fixes #49036