Skip to content

Commit

Permalink
[mono][HybridGlobalization] Fix ShortDatePattern year format to be "y…
Browse files Browse the repository at this point in the history
…yyy" (dotnet#99908)

* use yyyy year format for short date pattern

* add test for yyyy short date pattern

---------

Co-authored-by: Ilona Tomkowicz <32700855+ilonatommy@users.noreply.github.com>
  • Loading branch information
matouskozak and ilonatommy authored Apr 9, 2024
1 parent 79a4daf commit 404b286
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ private bool LoadCalendarDataFromNative(string localeName, CalendarId calendarId
sNativeName = GetCalendarInfoNative(localeName, calendarId, CalendarDataType.NativeName);
sMonthDay = GetCalendarInfoNative(localeName, calendarId, CalendarDataType.MonthDay);
saShortDates = GetCalendarInfoNative(localeName, calendarId, CalendarDataType.ShortDates).Split("||");
// Handle ShortDatePattern to have "yyyy" year format
List<string> shortDatePatternList = new List<string>(saShortDates);
for (int i = 0; i < shortDatePatternList.Count; i++)
{
shortDatePatternList[i] = NormalizeDatePattern(shortDatePatternList[i]);
}
FixDefaultShortDatePattern(shortDatePatternList);
saShortDates = shortDatePatternList.ToArray();

saLongDates = GetCalendarInfoNative(localeName, calendarId, CalendarDataType.LongDates).Split("||");
saYearMonths = GetCalendarInfoNative(localeName, calendarId, CalendarDataType.YearMonths).Split("||");
saDayNames = GetCalendarInfoNative(localeName, calendarId, CalendarDataType.DayNames).Split("||");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ namespace System.Globalization.Tests
{
public class DateTimeFormatInfoShortDatePattern
{
public static IEnumerable<object[]> ShortDatePattern_Get_TestData()
{
yield return new object[] { DateTimeFormatInfo.InvariantInfo, "MM/dd/yyyy", "invariant" };
yield return new object[] { new CultureInfo("en-US").DateTimeFormat, "M/d/yyyy", "en-US" };
yield return new object[] { new CultureInfo("fr-FR").DateTimeFormat, "dd/MM/yyyy", "fr-FR" };
}
public static IEnumerable<object[]> ShortDatePattern_Get_TestData_HybridGlobalization()
{
// see the comments on the right to check the non-Hybrid result, if it differs
Expand Down Expand Up @@ -131,7 +137,6 @@ public static IEnumerable<object[]> ShortDatePattern_Get_TestData_HybridGlobaliz
yield return new object[] { "en-ZA", "yyyy/MM/dd" };
yield return new object[] { "en-ZM", "dd/MM/yyyy" };
yield return new object[] { "en-ZW", "d/M/yyyy" };
yield return new object[] { "en-US", "M/d/yyyy" };
yield return new object[] { "es-419", "d/M/yyyy" };
yield return new object[] { "es-ES", "d/M/yyyy" };
yield return new object[] { "es-MX", "dd/MM/yyyy" };
Expand Down Expand Up @@ -200,6 +205,13 @@ public static IEnumerable<object[]> ShortDatePattern_Get_TestData_HybridGlobaliz
yield return new object[] { "zh-TW", "yyyy/M/d" };
}

[ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsHybridGlobalizationOnApplePlatform))]
[MemberData(nameof(ShortDatePattern_Get_TestData))]
public void ShortDatePattern_Get_ReturnsExpected(DateTimeFormatInfo format, string expected, string cultureName)
{
Assert.True(expected == format.ShortDatePattern, $"Failed for culture: {cultureName}. Expected: {expected}, Actual: {format.ShortDatePattern}");
}

[ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsHybridGlobalizationOnBrowser))]
[MemberData(nameof(ShortDatePattern_Get_TestData_HybridGlobalization))]
public void ShortDatePattern_Get_ReturnsExpected_HybridGlobalization(string cultureName, string expected)
Expand Down

0 comments on commit 404b286

Please sign in to comment.