Skip to content
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

[iOS/HybridGlobalization] Force short date pattern to use yyyy #99515

Closed
tipa opened this issue Mar 11, 2024 · 4 comments · Fixed by #99908
Closed

[iOS/HybridGlobalization] Force short date pattern to use yyyy #99515

tipa opened this issue Mar 11, 2024 · 4 comments · Fixed by #99908
Assignees
Milestone

Comments

@tipa
Copy link

tipa commented Mar 11, 2024

Description

When using HybridGlobalization on iOS, the shortdate format does only contain just either one or two y's, which result in only a two-digit year when formatting dates.

Reproduction Steps

Set <HybridGlobalization>true</HybridGlobalization> in iOS csproj file and format some date with "short date" format (e.g. DateTime.Now.ToString("d"))

Expected behavior

new CultureInfo("de-DE").DateTimeFormat.ShortDatePattern == "dd.MM.yyyy"
new CultureInfo("en-US").DateTimeFormat.ShortDatePattern == "M/d/yyyy"
new CultureInfo("fa-IR").DateTimeFormat.ShortDatePattern == "yyyy/M/d"

Actual behavior

new CultureInfo("de-DE").DateTimeFormat.ShortDatePattern == "dd.MM.yy"
new CultureInfo("en-US").DateTimeFormat.ShortDatePattern == "M/d/yy"
new CultureInfo("fa-IR").DateTimeFormat.ShortDatePattern == "y/M/d"

Regression?

No response

Known Workarounds

No response

Configuration

.NET 8

Other information

A similar problem was reported a while back for Linux here and fixed here. To code is still in the repo:

// FixDefaultShortDatePattern will convert the default short date pattern from using 'yy' to using 'yyyy'
// And will ensure the original pattern still exist in the list.
// doing that will have the short date pattern format the year as 4-digit number and not just 2-digit number.
// Example: June 5, 2018 will be formatted to something like 6/5/2018 instead of 6/5/18 fro en-US culture.
private static void FixDefaultShortDatePattern(List<string> shortDatePatterns)
{
if (shortDatePatterns.Count == 0)
return;
string s = shortDatePatterns[0];
// We are not expecting any pattern have length more than 100.
// We have to do this check to prevent stack overflow as we allocate the buffer on the stack.
if (s.Length > 100)
return;
Span<char> modifiedPattern = stackalloc char[s.Length + 2];
int index = 0;
while (index < s.Length)
{
if (s[index] == '\'')
{
do
{
modifiedPattern[index] = s[index];
index++;
} while (index < s.Length && s[index] != '\'');
if (index >= s.Length)
return;
}
else if (s[index] == 'y')
{
modifiedPattern[index] = 'y';
break;
}
modifiedPattern[index] = s[index];
index++;
}
if (index >= s.Length - 1 || s[index + 1] != 'y')
{
// not a 'yy' pattern
return;
}
if (index + 2 < s.Length && s[index + 2] == 'y')
{
// we have 'yyy' then nothing to do
return;
}
// we are sure now we have 'yy' pattern
Debug.Assert(index + 3 < modifiedPattern.Length);
modifiedPattern[index + 1] = 'y'; // second y
modifiedPattern[index + 2] = 'y'; // third y
modifiedPattern[index + 3] = 'y'; // fourth y
index += 2;
// Now, copy the rest of the pattern to the destination buffer
while (index < s.Length)
{
modifiedPattern[index + 2] = s[index];
index++;
}
shortDatePatterns[0] = modifiedPattern.ToString();
for (int i = 1; i < shortDatePatterns.Count; i++)
{
if (shortDatePatterns[i] == shortDatePatterns[0])
{
// Found match in the list to the new constructed pattern, then replace it with the original modified pattern
shortDatePatterns[i] = s;
return;
}
}
// if we come here means the newly constructed pattern not found on the list, then add the original pattern
shortDatePatterns.Add(s);
}

Copy link
Contributor

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

@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Mar 11, 2024
@tipa tipa changed the title [iOS/HybridGlobalization] Force short date pattern to use yyyy on iOS [iOS/HybridGlobalization] Force short date pattern to use yyyy Mar 11, 2024
@vitek-karas vitek-karas added the os-ios Apple iOS label Mar 11, 2024
@matouskozak matouskozak added this to the 9.0.0 milestone Mar 13, 2024
@matouskozak matouskozak removed the untriaged New issue has not been triaged by the area owner label Mar 18, 2024
@matouskozak
Copy link
Member

Thank you for catching this. I added fix in #99908.

@vitek-karas
Copy link
Member

Reopening to keep it active until it's actually fixed in 8 - #100810

@matouskozak
Copy link
Member

matouskozak commented May 20, 2024

The fix was successfully backported and the .NET 8.0.5 servicing release contains the fix.
Edit. the fix didn't make the 8.0.5 servicing but will be included in the next release.

@github-actions github-actions bot locked and limited conversation to collaborators Jul 21, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants