diff --git a/src/Uno.UI.Tests/Windows_Globalization/Given_CalendarFormatter.cs b/src/Uno.UI.Tests/Windows_Globalization/Given_CalendarFormatter.cs index 06b802b8c033..75a770a0f6ae 100644 --- a/src/Uno.UI.Tests/Windows_Globalization/Given_CalendarFormatter.cs +++ b/src/Uno.UI.Tests/Windows_Globalization/Given_CalendarFormatter.cs @@ -4,6 +4,8 @@ using FluentAssertions; using FluentAssertions.Execution; using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using System.Collections.Generic; namespace Uno.UI.Tests.Windows_Globalization { @@ -11,12 +13,12 @@ namespace Uno.UI.Tests.Windows_Globalization public class Given_CalendarFormatter { [TestMethod] - [DataRow("day month year", "en-US", "{month.numeric}/{day.integer}/{year.full}")] - [DataRow("day month year", "en-CA", "{year.full}-{month.numeric}-{day.integer(2)}")] - [DataRow("day month year", "en-GB", "{day.integer(2)}/{month.numeric}/{year.full}")] - [DataRow("day month year", "fr-CA", "{year.full}-{month.numeric}-{day.integer(2)}")] - [DataRow("day month year", "fr-FR", "{day.integer(2)}/{month.numeric}/{year.full}")] - [DataRow("day month year", "hu-HU", "{year.full}. {month.numeric}. {day.integer(2)}.")] + [DataRow("day month year", "en-US", "{month.integer}/{day.integer}/{year.full}")] + [DataRow("day month year", "en-CA", "{year.full}-{month.integer(2)}-{day.integer(2)}")] + [DataRow("day month year", "en-GB", "{day.integer(2)}/{month.integer(2)}/{year.full}")] + [DataRow("day month year", "fr-CA", "{year.full}-{month.integer(2)}-{day.integer(2)}")] + [DataRow("day month year", "fr-FR", "{day.integer(2)}/{month.integer(2)}/{year.full}")] + [DataRow("day month year", "hu-HU", "{year.full}. {month.integer(2)}. {day.integer(2)}.")] public void When_UsingVariousLanguages(string format, string language, string expectedPattern) { var sut = new DateTimeFormatter(format, new[] { language }); @@ -24,6 +26,33 @@ public void When_UsingVariousLanguages(string format, string language, string ex firstPattern.Should().Be(expectedPattern); } + [TestMethod] + public void When_FormattingDateVariants_ShouldProduceExpectedFormats() + { + var expectedResults = new Dictionary + { + { "{day.integer}/{month.integer}/{year.full}", "27/6/2024" }, + { "{month.full} {year.full}", "June 2024" }, + { "{month.full} {day.integer}", "June 27" }, + { "{dayofweek.abbreviated}, {day.integer} {month.abbreviated} {year.full}", "Thu, 27 Jun 2024" }, + { "{year.full}-{month.integer}-{day.integer}", "2024-6-27" }, + { "{day.integer}/{month.integer}/{year.abbreviated}", "27/6/24" }, + { "{day.integer} {month.abbreviated} {year.full}", "27 Jun 2024" }, + { "{month.full} {day.integer}, {year.full}", "June 27, 2024" }, + { "{month.abbreviated} {day.integer}, {year.full}", "Jun 27, 2024" }, + { "{dayofweek.full}, {day.integer} {month.full} {year.full}", "Thursday, 27 June 2024" }, + { "{dayofweek.abbreviated}, {day.integer} {month.abbreviated} {year.abbreviated}", "Thu, 27 Jun 24" } + }; + foreach (var kvp in expectedResults) + { + var template = kvp.Key; + var expected = kvp.Value; + var formatter = new DateTimeFormatter(template); + var formattedDate = formatter.Format(new(2024, 6, 27, 14, 30, 0, TimeSpan.Zero)); + Assert.AreEqual(expected, formattedDate, $"Mismatch for template: {template}"); + } + } + #if !NET7_0_OR_GREATER // https://github.com/unoplatform/uno/issues/9080 [TestMethod] [DataRow("day", "en-US|fr-CA|ru-RU", "{day.integer}|{day.integer}|{day.integer}")] diff --git a/src/Uno.UWP/Globalization/DateTimeFormatting/DateTimeFormatter.cs b/src/Uno.UWP/Globalization/DateTimeFormatting/DateTimeFormatter.cs index c354b603d727..bc00ca60796a 100644 --- a/src/Uno.UWP/Globalization/DateTimeFormatting/DateTimeFormatter.cs +++ b/src/Uno.UWP/Globalization/DateTimeFormatting/DateTimeFormatter.cs @@ -623,7 +623,7 @@ void AddToBuilder(StringBuilder builder, char lastChar, int count) while (count >= 4) { - builder.Append("{year.full(4)}"); + builder.Append("{year.full}"); count -= 4; } @@ -668,7 +668,7 @@ void AddToBuilder(StringBuilder builder, char lastChar, int count) while (count >= 1) { - builder.Append("{month.integer(1)}"); + builder.Append("{month.integer}"); count -= 1; } @@ -695,7 +695,7 @@ void AddToBuilder(StringBuilder builder, char lastChar, int count) while (count >= 1) { - builder.Append("{day.integer(1)}"); + builder.Append("{day.integer}"); count -= 1; }