Skip to content

Conversation

pra2892
Copy link
Contributor

@pra2892 pra2892 commented Apr 2, 2025

Fix DateTime.TryParseExact for "MMM.yy" format in German culture

This commit fixes an issue where DateTime.TryParseExact fails to parse dates formatted with "MMM.yy" in German culture (de-DE). The problem occurs because the parsing logic incorrectly tries to match against genitive month names when parsing abbreviated month names without day specifiers.

According to the documentation, genitive month names should only be used when the format pattern contains day specifiers ('d' or 'dd'). The "MMM.yy" format doesn't contain day specifiers, so regular month names should be used.

Modified the MatchAbbreviatedMonthName method to conditionally use genitive month names only when appropriate, fixing scenarios like:

// s = "Dez.25"
DateTime.TryParseExact(s, "MMM.yy", new CultureInfo("de-DE"), DateTimeStyles.None, out var dt);
// Now returns true instead of false

Fixes #114159

@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Apr 2, 2025
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-globalization
See info in area-owners.md if you want to be subscribed.

@pra2892
Copy link
Contributor Author

pra2892 commented Apr 2, 2025

@dotnet-policy-service agree

@dotnet-policy-service agree

@tarekgh tarekgh added this to the 10.0.0 milestone Apr 2, 2025
@tarekgh tarekgh self-assigned this Apr 2, 2025
@tarekgh
Copy link
Member

tarekgh commented Apr 2, 2025

@pra2892 thanks for submitting the PR. I am seeing you added submodule called runtime to this PR. Please delete it.

// Search genitive form.
if ((dtfi.FormatFlags & DateTimeFormatFlags.UseGenitiveMonth) != 0)
// Only search genitive form if useGenitiveForm is true
if ((dtfi.FormatFlags & DateTimeFormatFlags.UseGenitiveMonth) != 0 && useGenitiveForm)
Copy link
Member

Choose a reason for hiding this comment

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

useGenitiveForm

I am not sure I understand this fix. the value of useGenitiveForm is always false so the condition will always be evaluated as false.

The fix should be, passing the format string to this method MatchAbbreviatedMonthName. and when checking
dtfi.FormatFlags & DateTimeFormatFlags.UseGenitiveMonth) != 0, can check if format string has d or dd specifiers.

@pra2892 pra2892 closed this Apr 2, 2025
pra2892 added a commit to pra2892/runtime that referenced this pull request Apr 2, 2025
pra2892 added a commit to pra2892/runtime that referenced this pull request Apr 2, 2025
pra2892 added a commit to pra2892/runtime that referenced this pull request Apr 3, 2025
pra2892 added a commit to pra2892/runtime that referenced this pull request Apr 3, 2025
pra2892 added a commit to pra2892/runtime that referenced this pull request Apr 3, 2025
pra2892 added a commit to pra2892/runtime that referenced this pull request Apr 4, 2025
pra2892 added a commit to pra2892/runtime that referenced this pull request Apr 4, 2025
tarekgh added a commit that referenced this pull request Apr 12, 2025
* Fix "MMM.yy" date parsing in German culture. #114169

* Fix "MMM.yy" date parsing in German culture. #114169

* Fix "MMM.yy" date parsing in German culture. #114169 v3

* Fix "MMM.yy" date parsing in German culture. #114169 [Testing]

* Fix "MMM.yy" date parsing in German culture. #114169 [Testing] v1

* Fix the test

---------

Co-authored-by: Tarek Mahmoud Sayed <[email protected]>
@github-actions github-actions bot locked and limited conversation to collaborators May 3, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-System.Globalization community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TryParseExact not working as expected

2 participants