Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Apr 12, 2025
Merged

Conversation

pra2892
Copy link
Contributor

@pra2892 pra2892 commented Apr 3, 2025

This solution handles all the edge cases:

  • The FormatContainsDayOfMonthSpecifier method properly skips 'd' characters inside quotes.
  • Both \ and % escape characters are handled.
  • We only consider 'd' or 'dd' as day-of-month specifiers, not 'ddd' or 'dddd' (day-of-week).
  • We analyze the entire format string, so it doesn't matter if 'MMM' comes before or after 'd'.
  • The entire format is analyzed, so multiple day or month patterns are handled correctly.
  • We only scan the format string once, and only when needed.

This solution should work correctly in all scenarios, including the specific case mentioned with "MMM dd" format in German culture.

@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Apr 3, 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.

@lilinus
Copy link
Contributor

lilinus commented Apr 3, 2025

It would probably be good with some tests for this kind of case.

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

tarekgh commented Apr 3, 2025

@pra2892 thanks again for submitting the PR. This looks good. We need to add some tests to ensure we'll not break the behavior in the future. I would suggest in the test you create a culture like the following and ensure setting culture.DateTimeFormat.AbbreviatedMonthGenitiveNames to guarantee the test will never fail in the future if the culture data changed.

 var culture = new CultureInfo("de-DE");
culture.DateTimeFormat.AbbreviatedMonthGenitiveNames = ["Jan.", "Feb.", "März", "Apr.", "Mai", "Juni", "Juli", "Aug.", "Sept", "Okt.", "Nov.", "Dez.", ""];
culture.DateTimeFormat.MonthGenitiveNames = ["Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember.", ''"];
culture.DateTimeFormat.DayNames = ["Sonntag", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag"];
culture.DateTimeFormat.DateSeparator = ".";

Then you add test cases:

  • Parse exact Dez.20 with the format MMM.yy and ensure the parsing succeeds.
  • parse exact Dez.20 01 with the format MMM.yy dd and ensure the parsing fails.
  • parse exact 01 Dez.20 with the format d MMM.yy and ensure the parsing fails.
  • parse exact Dienstag Dez.20 with the format dddd MMM.yy and ensure the parsing succeed.
  • then repeat the same tests with patterns using MMMM and month name Dezember.

You need to add the tests to the file https://github.com/dotnet/runtime/blob/main/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/DateTimeTests.cs

@ANahr
Copy link
Contributor

ANahr commented Apr 3, 2025

Two small "corrections"/improvements (especially "Sept." because it is the only one with 5 chars):

var culture = new CultureInfo("de-DE");
culture.DateTimeFormat.AbbreviatedMonthGenitiveNames = ["Jan.", "Feb.", "März", "Apr.", "Mai", "Juni", "Juli", "Aug.", "Sept.", "Okt.", "Nov.", "Dez.", ""];
culture.DateTimeFormat.MonthGenitiveNames = ["Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember", ''"];
culture.DateTimeFormat.DayNames = ["Sonntag", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag"];
culture.DateTimeFormat.DateSeparator = ".";

@tarekgh
Copy link
Member

tarekgh commented Apr 3, 2025

@ANahr I intentionally used dot at the end of the Dezember. so we can successfully test with the date patterns we are using. Fixing any other month names other than December is irrelevant if we are always testing with December.

@tarekgh
Copy link
Member

tarekgh commented Apr 7, 2025

@pra2892 did you have a chance to address my comment #114194 (review)?

@tarekgh
Copy link
Member

tarekgh commented Apr 9, 2025

@pra2892 I have pushed the test fix to this PR so we can merge it.

@tarekgh
Copy link
Member

tarekgh commented Apr 12, 2025

/ba-g the failure is already tracked with #114465

@tarekgh tarekgh merged commit 36f59e8 into dotnet:main Apr 12, 2025
139 of 142 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators May 13, 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.

4 participants