Throw ArgumentException for invalid DateTimeStyles in TryParse/TryParseExact#122
Merged
Merged
Conversation
…seExact DateOnly/TimeOnly.TryParse and TryParseExact (span-based overloads) silently returned false for a DateTimeStyles value outside AllowWhiteSpaces, instead of throwing like Parse/ParseExact already do and like upstream dotnet/runtime's DateOnly/TimeOnly.TryParse now does. Add the same up-front validation used by Parse/ParseExact to the three span-based Try* entry points on both types. Fixes #117
There was a problem hiding this comment.
Pull request overview
This PR aligns DateOnly/TimeOnly span-based TryParse/TryParseExact behavior with Parse/ParseExact (and upstream dotnet/runtime) by throwing ArgumentException when DateTimeStyles contains unsupported flags, instead of silently returning false.
Changes:
- Added upfront
(style & ~DateTimeStyles.AllowWhiteSpaces) != 0validation (throwingArgumentException) to the span-basedTryParse/TryParseExactentry points for bothDateOnlyandTimeOnly. - Updated/expanded tests to expect
ArgumentExceptionfor invalid styles in the affectedTry*overloads and added coverage for previously untested paths.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| DateTimeOnly/TimeOnly.cs | Adds upfront invalid-DateTimeStyles checks to span-based TryParse/TryParseExact overloads so invalid styles throw consistently. |
| DateTimeOnly/DateOnly.cs | Same upfront invalid-DateTimeStyles checks for DateOnly span-based TryParse/TryParseExact overloads. |
| DateTimeOnly.Tests/TimeOnlyTests.cs | Updates a pre-existing test to assert invalid styles throw for TimeOnly.TryParse (matching Parse). |
| DateTimeOnly.Tests/DateOnlyTests.cs | Updates a pre-existing test to assert invalid styles throw for DateOnly.TryParse (matching Parse). |
| DateTimeOnly.Tests/ArgumentValidationTests.cs | Updates existing assertions to expect ArgumentException (not false) and adds new coverage for invalid-style TryParse/TryParseExact paths. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #117.
DateOnly/TimeOnlyTryParseandTryParseExact(span-based overloads) silently returnedfalsefor aDateTimeStylesvalue outsideAllowWhiteSpaces, instead of throwingArgumentExceptionlikeParse/ParseExactalready do and like upstreamdotnet/runtime'sDateOnly/TimeOnly.TryParsenow does.(style & ~DateTimeStyles.AllowWhiteSpaces) != 0check used byParse/ParseExactto the three span-basedTry*entry points on both types:TryParse(ReadOnlySpan<char>, IFormatProvider?, DateTimeStyles, out T)TryParseExact(ReadOnlySpan<char>, ReadOnlySpan<char>, IFormatProvider?, DateTimeStyles, out T)TryParseExact(ReadOnlySpan<char>, string?[]?, IFormatProvider?, DateTimeStyles, out T)string-based overloads delegate into these, so they pick up the fix automatically once theirs/format/formatsnull checks pass.DateTimeStylesvalidation gap (TryParse/TryParseExact should throw for invalid DateTimeStyles, not return false #117). The separateformats[]up-front validation gap (ParseExact/TryParseExact(formats[]) should validate the whole array up front #118) and the malformed-format-string gap (TryParseExact silently returns false for a malformed custom format string instead of throwing #119) are not touched here.Tests
DateOnlyTests.cs/TimeOnlyTests.cs(BasicFormatParseTest) that literally asserted the old buggyTryParsebehavior (Assert.False(...)) right next to a correctParseassertion (Assert.Throws<ArgumentException>(...)) for the same invalid style — now both assert the throw.ArgumentValidationTests.cs: two existingTryParseExactassertions that used an invalid style (AssumeUniversal/AssumeLocal) with non-null input now expectArgumentExceptioninstead offalse.TryParse(span/string)andTryParseExact(formats[])overloads with an invalid style, since none of those specific paths had a non-null-input test before.Test plan
dotnet build -c Debug— succeeds, 0 warnings/errorsdotnet test— 169/169 passed