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

chore: Add missing DateTimeFormatter formats #18948

Merged
merged 3 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,20 @@
<ComboBoxItem Content="{}{dayofweek.solo.abbreviated(2)}"></ComboBoxItem>
<ComboBoxItem Content="{}{dayofweek.solo.abbreviated(3)}"></ComboBoxItem>
</ComboBox>
<ComboBox x:Name="dateformat" Header="DateFormat">
<ComboBoxItem Content="{}{day.integer}/{month.integer}/{year.full}"></ComboBoxItem>
<ComboBoxItem Content="{}{month.full} {year.full}"></ComboBoxItem>
<ComboBoxItem Content="{}{month.full} {day.integer}"></ComboBoxItem>
<ComboBoxItem Content="{}{dayofweek.full}, {day.integer} {month.full} {year.full}"></ComboBoxItem>
<ComboBoxItem Content="{}{dayofweek.abbreviated}, {day.integer} {month.abbreviated} {year.full}"></ComboBoxItem>
<ComboBoxItem Content="{}{year.full}-{month.integer}-{day.integer}"></ComboBoxItem>
<ComboBoxItem Content="{}{day.integer}/{month.integer}/{year.abbreviated}"></ComboBoxItem>
<ComboBoxItem Content="{}{day.integer} {month.abbreviated} {year.full}"></ComboBoxItem>
<ComboBoxItem Content="{}{month.full} {day.integer}, {year.full}"></ComboBoxItem>
<ComboBoxItem Content="{}{month.abbreviated} {day.integer}, {year.full}"></ComboBoxItem>
<ComboBoxItem Content="{}{dayofweek.full}, {day.integer} {month.full} {year.full}"></ComboBoxItem>
<ComboBoxItem Content="{}{dayofweek.abbreviated}, {day.integer} {month.abbreviated} {year.abbreviated}"></ComboBoxItem>
</ComboBox>
<ComboBox x:Name="cid" Header="Calendar">
<ComboBoxItem>ChineseLunarCalendar</ComboBoxItem>
<ComboBoxItem IsSelected="True">GregorianCalendar</ComboBoxItem>
Expand All @@ -55,6 +69,7 @@
CalendarIdentifier="{Binding SelectedItem.Content, ElementName=cid, FallbackValue=GregorianCalendar}"
FirstDayOfWeek="{Binding SelectedItem.Content, ElementName=dow, FallbackValue=Sunday}"
DayOfWeekFormat="{Binding SelectedItem.Content, ElementName=dowf}"
DateFormat="{Binding SelectedItem.Content, ElementName=dateformat}"
IsTodayHighlighted="{Binding IsChecked, ElementName=today}"/>
<ToggleButton IsChecked="{Binding IsEnabled, ElementName=cdp1, Mode=TwoWay}">
IsEnabled
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Windows.Globalization.DateTimeFormatting;
using System.Globalization;
using Windows.Globalization;
using System.Linq;
using System.Collections.Generic;

namespace Uno.UI.RuntimeTests.Tests.Windows_Globalization;

[TestClass]
public class Given_DateTimeFormatter
{
[TestMethod]
public void When_CreatingWithFormatTemplate_ShouldInitializeCorrectly()
{
var formatter = new DateTimeFormatter("longdate");
Assert.IsNotNull(formatter);
Assert.AreEqual("longdate", formatter.Template);
}

[TestMethod]
public void When_UsingCustomLanguages_ShouldSetLanguagesCorrectly()
{
var languages = new[] { "fr-FR", "en-US" };
var formatter = new DateTimeFormatter("longdate", languages);

CollectionAssert.AreEqual(languages, formatter.Languages.ToList());
}

[TestMethod]
public void When_FormattingDateTimeOffset_ShouldProduceFormattedString()
{
var formatter = new DateTimeFormatter("longdate");
var dateToFormat = new DateTimeOffset(2024, 1, 15, 0, 0, 0, TimeSpan.Zero);

var formattedDate = formatter.Format(dateToFormat);
Assert.IsNotNull(formattedDate);
Assert.IsTrue(formattedDate.Length > 0);
}

[TestMethod]
public void When_SettingNumeralSystem_ShouldUpdateProperty()
{
var formatter = new DateTimeFormatter("longdate");
formatter.NumeralSystem = "arab";

Assert.AreEqual("arab", formatter.NumeralSystem);
}

[TestMethod]
public void When_CheckingPatterns_ShouldNotBeEmpty()
{
var formatter = new DateTimeFormatter("longdate");

Assert.IsTrue(formatter.Patterns.Count > 0);
}

[TestMethod]
public void When_CallingFormatWithTimeZone_ShouldThrowNotSupportedException()
{
var formatter = new DateTimeFormatter("longdate");
var dateToFormat = new DateTimeOffset(2024, 1, 15, 0, 0, 0, TimeSpan.Zero);

Assert.ThrowsException<NotSupportedException>(() =>
formatter.Format(dateToFormat, "UTC"));
}

[TestMethod]
public void When_CreatingFormatter_ShouldUseApplicationLanguages()
{
var formatter = new DateTimeFormatter("longdate");

CollectionAssert.AreEqual(ApplicationLanguages.Languages.ToList(), formatter.Languages.ToList());
}

[TestMethod]
public void When_FormattingDateVariants_ShouldProduceExpectedFormats()
{
var expectedResults = new Dictionary<string, string>
{
{ "{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}");
}
}

// TODO(DT): Currently is throwing unexpected results on WinUI as well, can't compare the expected results
/* [TestMethod]
public void When_FormattingTimeVariants_ShouldProduceExpectedFormats()
{
var expectedResults = new Dictionary<string, string>
{
{ "{hour.integer}:{minute.integer}", "14:30" },
{ "{hour.integer}:{minute.integer}:{second.integer}", "14:30:00" },
{ "{hour.integer}:{minute.integer} {period.abbreviated(2)}", "2:30 PM" },
{ "{hour.integer}:{minute.integer}:{second.integer} {period.abbreviated(2)}", "2:30:00 PM" },
{ "{minute.integer}:{second.integer}", "30:00" },
{ "{second.integer}", "0" },
{ "{hour.integer}:{minute.integer}:{second.integer} UTC", "14:30:00 UTC" }
};

foreach (var kvp in expectedResults)
{
var template = kvp.Key;
var expected = kvp.Value;

var formatter = new DateTimeFormatter(template);
var formattedTime = formatter.Format(_testDate);

Assert.AreEqual(expected, formattedTime, $"Mismatch for template: {template}");
Console.WriteLine($"Template: {template}, Result: {formattedTime}");
}
}*/
}
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,9 @@ private IDictionary<string, string> BuildLookup(string language)
{ "timezone.abbreviated" , "zz" },
{ "timezone.full" , "zzz" },
{ "dayofweek" , "dddd" } ,
{ "day.integer" , "d" },
MartinZikmund marked this conversation as resolved.
Show resolved Hide resolved
{ "day" , "%d" } ,
{ "month.integer" , "M" },
{ "month" , "MMMM" } ,
{ "year" , "yyyy" } ,
{ "hour.integer(1)" , "%h" },
Expand Down
Loading