Skip to content

Throw ArgumentException for invalid DateTimeStyles in TryParse/TryParseExact#122

Merged
OlegRa merged 1 commit into
masterfrom
fix/117-tryparse-invalid-datetimestyles
Jul 11, 2026
Merged

Throw ArgumentException for invalid DateTimeStyles in TryParse/TryParseExact#122
OlegRa merged 1 commit into
masterfrom
fix/117-tryparse-invalid-datetimestyles

Conversation

@OlegRa

@OlegRa OlegRa commented Jul 11, 2026

Copy link
Copy Markdown
Owner

Summary

Closes #117.

DateOnly/TimeOnly TryParse and TryParseExact (span-based overloads) silently returned false for a DateTimeStyles value outside AllowWhiteSpaces, instead of throwing ArgumentException like Parse/ParseExact already do and like upstream dotnet/runtime's DateOnly/TimeOnly.TryParse now does.

Tests

  • Fixed two pre-existing tests in DateOnlyTests.cs/TimeOnlyTests.cs (BasicFormatParseTest) that literally asserted the old buggy TryParse behavior (Assert.False(...)) right next to a correct Parse assertion (Assert.Throws<ArgumentException>(...)) for the same invalid style — now both assert the throw.
  • Updated ArgumentValidationTests.cs: two existing TryParseExact assertions that used an invalid style (AssumeUniversal/AssumeLocal) with non-null input now expect ArgumentException instead of false.
  • Added new coverage for the TryParse(span/string) and TryParseExact(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/errors
  • dotnet test — 169/169 passed

…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
@OlegRa OlegRa requested a review from Copilot July 11, 2026 08:44
@OlegRa OlegRa self-assigned this Jul 11, 2026
@OlegRa OlegRa added the bug Something isn't working label Jul 11, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) != 0 validation (throwing ArgumentException) to the span-based TryParse/TryParseExact entry points for both DateOnly and TimeOnly.
  • Updated/expanded tests to expect ArgumentException for invalid styles in the affected Try* 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.

@OlegRa OlegRa merged commit 6520cb6 into master Jul 11, 2026
7 checks passed
@OlegRa OlegRa deleted the fix/117-tryparse-invalid-datetimestyles branch July 11, 2026 08:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TryParse/TryParseExact should throw for invalid DateTimeStyles, not return false

2 participants