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

fix: Improve DateTimeFormatter template matching and mappings #19099

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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 @@ -104,31 +104,31 @@
}
}

// TODO(DT): Currently is throwing unexpected results on WinUI as well, can't compare the expected results
/* [TestMethod]
[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}:{second.integer(2)}", "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" },
{ "{hour.integer}:{minute.integer}:{second.integer} {period.abbreviated(2)}", "2:30:0 PM" },
{ "{minute.integer}:{second.integer}", "30:0" },
{ "{second.integer}", "0" },
{ "{hour.integer}:{minute.integer}:{second.integer} UTC", "14:30:00 UTC" }
{ "{hour.integer}:{minute.integer}:{second.integer(2)} 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);
var clock = template.Contains("period") ? ClockIdentifiers.TwelveHour : ClockIdentifiers.TwentyFourHour;

Check notice on line 126 in src/Uno.UI.RuntimeTests/Tests/Windows_Globalization/Given_DateTimeFormatter.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/Uno.UI.RuntimeTests/Tests/Windows_Globalization/Given_DateTimeFormatter.cs#L126

Change this call to 'template.Contains' to an overload that accepts a 'StringComparison' as a parameter.

var formatter = new DateTimeFormatter(template, ["en-US"], "US", CalendarIdentifiers.Gregorian, clock);
var formattedTime = formatter.Format(new DateTime(2024, 6, 17, 14, 30, 0));

Assert.AreEqual(expected, formattedTime, $"Mismatch for template: {template}");
Console.WriteLine($"Template: {template}, Result: {formattedTime}");
}
}*/
}
}
100 changes: 76 additions & 24 deletions src/Uno.UWP/Globalization/DateTimeFormatting/DateTimeFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -303,43 +303,90 @@
var info = new CultureInfo(language).DateTimeFormat;

map = new Dictionary<string, string>
{
{
// Predefined patterns
{ "longdate" , info.LongDatePattern } ,
{ "shortdate" , info.ShortDatePattern } ,
{ "longtime" , info.LongTimePattern } ,
{ "shorttime" , info.ShortTimePattern } ,

// Compound patterns
{ "dayofweek day month year" , info.FullDateTimePattern } ,
{ "dayofweek day month" , "D" } ,
{ "day month year" , info.ShortDatePattern } ,
{ "day month.full year" , info.ShortDatePattern } ,
{ "day month" , info.MonthDayPattern } ,
{ "month year" , info.YearMonthPattern } ,
{ "dayofweek.full" , "dddd" } ,
{ "dayofweek.abbreviated" , "ddd" } ,
{ "month.full" , "MMMM" } ,
{ "month.abbreviated" , "MMM" } ,
{ "month.numeric" , "%M" } ,
{ "year.abbreviated" , "yy" } ,
{ "year.full" , "yyyy" } ,
{ "hour minute second" , info.LongTimePattern },
{ "hour minute" , info.ShortTimePattern },
{ "timezone.abbreviated" , "zz" },
{ "timezone.full" , "zzz" },
//{ "year month day hour" , "" },

// Day of week formats
{ "dayofweek" , "dddd" } ,
{ "day.integer" , "d" },
{ "dayofweek.full" , "dddd" } ,
{ "dayofweek.abbreviated" , "ddd" } ,
{ "dayofweek.abbreviated(1)" , "dd" } ,
{ "dayofweek.abbreviated(2)" , "ddd" } ,
{ "dayofweek.solo.full" , "dddd" } ,
{ "dayofweek.solo.abbreviated" , "ddd" } ,

// Day formats
{ "day" , "%d" } ,
{ "month.integer" , "M" },
{ "day.integer" , "%d" },
{ "day.integer(1)" , "%d" },
{ "day.integer(2)" , "dd" },

// Month formats
{ "month" , "MMMM" } ,
{ "month.full" , "MMMM" } ,
{ "month.abbreviated" , "MMM" } ,
{ "month.abbreviated(1)" , "%M" } ,
{ "month.abbreviated(2)" , "MMM" } ,
{ "month.numeric" , "%M" } ,
{ "month.integer" , "%M" } ,
{ "month.integer(1)" , "%M" } ,
{ "month.integer(2)" , "MM" } ,
{ "month.solo.full" , "MMMM" } ,
{ "month.solo.abbreviated" , "MMM" } ,

// Year formats
{ "year" , "yyyy" } ,
{ "hour.integer(1)" , "%h" },
{ "hour" , "H tt" } ,
{ "minute.integer(2)" , "mm" },
{ "minute.integer" , "%m" },
{ "minute" , "%m" },
{ "second" , "%s" },
{ "timezone" , "%z" },
{ "period.abbreviated(2)" , "tt" },
// { "year month day hour" , "" } ,
{ "year.full" , "yyyy" } ,
{ "year.abbreviated" , "yy" } ,
{ "year.abbreviated(1)" , "%y" } ,
{ "year.abbreviated(2)" , "yy" } ,

// Hour formats
{ "hour" , "%H" } ,
{ "hour.integer" , "%H" } ,
{ "hour.integer(1)" , "%h" } ,
{ "hour.integer(2)" , "HH" } ,

// Period (AM/PM) formats
{ "period" , "tt" } ,
{ "period.full" , "tt" } ,
{ "period.abbreviated" , "tt" } ,
{ "period.abbreviated(1)" , "t" } ,
{ "period.abbreviated(2)" , "tt" } ,

// Minute formats
{ "minute" , "%m" } ,
{ "minute.integer" , "%m" } ,
{ "minute.integer(1)" , "%m" } ,
{ "minute.integer(2)" , "mm" } ,

// Second formats
{ "second" , "%s" } ,
{ "second.integer" , "%s" } ,
{ "second.integer(1)" , "%s" } ,
{ "second.integer(2)" , "ss" } ,

// Timezone formats
{ "timezone" , "%z" } ,
{ "timezone.full" , "zzz" } ,
{ "timezone.abbreviated" , "zz" } ,
{ "timezone.abbreviated(1)" , "%z" } ,
{ "timezone.abbreviated(2)" , "zz" }
};

return _mapCache[language] = map;
Expand Down Expand Up @@ -370,16 +417,21 @@

var map = _maps![0];

foreach (var p in map)
{
result = result.Replace(p.Key, p.Value);
var sortedKeys = map.Keys.OrderByDescending(k => k.Length);

foreach (var key in sortedKeys)
{
result = result.Replace(key, map[key]);
}

if (result.Contains("h") && Clock == ClockIdentifiers.TwentyFourHour)
{
result = result.Replace("h", "H");
}
else if (result.Contains("H") && Clock == ClockIdentifiers.TwelveHour)

Check notice on line 431 in src/Uno.UWP/Globalization/DateTimeFormatting/DateTimeFormatter.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/Uno.UWP/Globalization/DateTimeFormatting/DateTimeFormatter.cs#L431

Change this call to 'result.Contains' to an overload that accepts a 'StringComparison' as a parameter.
{
result = result.Replace("H", "h");

Check notice on line 433 in src/Uno.UWP/Globalization/DateTimeFormatting/DateTimeFormatter.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/Uno.UWP/Globalization/DateTimeFormatting/DateTimeFormatter.cs#L433

Change this call to 'result.Replace' to an overload that accepts a 'StringComparison' as a parameter.
}

return result;
}
Expand Down
Loading