-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Fix the ICU time format conversion logic #103681
Fix the ICU time format conversion logic #103681
Conversation
Revise the ICU time format conversion logic to support all unquoted literal texts and the `B` and `b` pattern symbols. Fix dotnet#103592
I'm new to such a big and complex project and not sure how to test these changes, so this is a draft at the moment. Looking forward to help from anyone. Example affected patterns:
More information can be found in the fixing issue. |
@PopSlime thanks for submitting the PR. |
/azp run runtime-extra-platforms |
Azure Pipelines successfully started running 1 pipeline(s). |
@mkhamoyan I'm not sure how to write the test for this because the retrieved time format is device-/platform-dependent. Instead of testing against some specific patterns, I'm considering adding a test that fails if a 12-hour clock is used without an AM/PM designator in any pattern in any culture. Also we need another test for the unquoted literal texts, which I have no clues how to do yet. By the way, short time patterns are also affected by this PR. |
@PopSlime I was thinking something like below (same test case for shorttime patterns under here) would validate the change.
This should be consistent for all platforms and devices. |
@mkhamoyan Is it safe to assume those cultures use a 12-hour clock? |
My bad, |
How about this? [Fact]
public void LongTimePattern_VerifyTimePatterns()
{
foreach (var culture in CultureInfo.GetCultures(CultureTypes.AllCultures))
{
var pattern = culture.DateTimeFormat.LongTimePattern;
var segments = pattern.Split('\'');
bool use12Hour = false;
bool useAMPM = false;
for (var i = 0; i < segments.Length; i += 2)
{
var segment = segments[i];
use12Hour |= segment.Contains('h', StringComparison.Ordinal);
useAMPM |= segment.Contains('t', StringComparison.Ordinal);
}
Assert.True(!use12Hour || useAMPM, $"Bad time pattern for culture {culture.Name}: '{pattern}'");
}
} By the way, this fails on my PC (Windows) with |
Looks good to me.
Yes , we should file an issue for that. |
src/libraries/System.Private.CoreLib/src/System/Globalization/CultureData.Icu.cs
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/Globalization/CultureData.Icu.cs
Outdated
Show resolved
Hide resolved
Add tests verifying that all the short and long time patterns either use a 24-hour clock or have an AM/PM designator.
@dotnet-policy-service agree |
I don't think we can fix that as the patterns are picked from Windows. I suggest you mark your new test methods with the attribute and see if this can help? Line 264 in 7934e64
|
I'll do that, but I also want to see if similar issues are occurring on other platforms, so let's wait for these checks to be done. |
I am trying on my machine, and I am not getting the same results you are getting.
running code like: var ci = CultureInfo.GetCultureInfo("mi-NZ");
Console.WriteLine($"{ci.Name} .. {ci.EnglishName} .. {ci.DateTimeFormat.ShortTimePattern} .. {ci.DateTimeFormat.LongTimePattern}");
ci = CultureInfo.GetCultureInfo("mi");
Console.WriteLine($"{ci.Name} .. {ci.EnglishName} .. {ci.DateTimeFormat.ShortTimePattern} .. {ci.DateTimeFormat.LongTimePattern}"); What Windows version do you have? I am wondering how you get this result? |
It seems that the version of the CLDR data installed on my PC is below 36. The time format for
|
ok, then the attribute Line 264 in 7934e64
Line 10 in 2a779ab
I meant using check like |
Current behavior was expected and other |
/azp run runtime-ioslike,runtime-ioslikesimulator,runtime-maccatalyst |
Azure Pipelines successfully started running 3 pipeline(s). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me.
Let's wait for ios pipelines and then we can merge.
src/libraries/System.Private.CoreLib/src/System/Globalization/CultureData.Icu.cs
Outdated
Show resolved
Hide resolved
/azp run runtime-ioslike,runtime-ioslikesimulator,runtime-maccatalyst |
Azure Pipelines successfully started running 3 pipeline(s). |
/azp run runtime |
Commenter does not have sufficient privileges for PR 103681 in repo dotnet/runtime |
/azp run runtime |
Azure Pipelines successfully started running 1 pipeline(s). |
* Fix the ICU time format conversion logic Revise the ICU time format conversion logic to support all unquoted literal texts and the `B` and `b` pattern symbols. Fix dotnet#103592 * Clarify literal texts in the conversion logic * Add tests for verifying time patterns Add tests verifying that all the short and long time patterns either use a 24-hour clock or have an AM/PM designator. * Fix literal single quote and literal backslash conversion * Refactor the literal quote conversion logic * Revise the test logic to ignore literal texts and check pattern redundancy Modify the test logic so that it recognizes literal texts correctly, and fails if 12-hour and 24-hour clocks are used at the same time. * Revise the test logic to ensure all cultures are tested * Add comments to clarify the backslash conversion * Refactor the conversion logic Simplify some logic and improve readability. * Exclude bad ICU patterns from the tests * Exclude the VerifyTimePatterns tests from hybrid globalization on browser * Add missing usings * Improve readability of the for-loops
Revise the ICU time format conversion logic to support all unquoted literal texts and the
B
andb
pattern symbols.Fix #103592