11using Microsoft . UI . Xaml ;
22using Microsoft . UI . Xaml . Controls ;
33using System ;
4+ using System . Collections . Generic ;
45using System . Diagnostics ;
6+ using System . Globalization ;
57using Windows . Globalization . DateTimeFormatting ;
68using Windows . System . UserProfile ;
79using WinUI . TableView . Extensions ;
810
911namespace WinUI . TableView . Helpers ;
1012
1113/// <summary>
12- /// Provides helper methods for formatting Date& Time values.
14+ /// Provides helper methods for formatting Date and Time values.
1315/// </summary>
1416internal static class DateTimeFormatHelper
1517{
1618 private const string _12HourClock = "12HourClock" ;
1719 private const string _24HourClock = "24HourClock" ;
18-
19- /// <summary>
20- /// Gets the DateTimeFormatter for 12-hour clock format.
21- /// </summary>
22- internal static DateTimeFormatter _12HourClockFormatter { get ; } = GetClockFormatter ( _12HourClock ) ;
23-
24- /// <summary>
25- /// Gets the DateTimeFormatter for 24-hour clock format.
26- /// </summary>
27- internal static DateTimeFormatter _24HourClockFormatter { get ; } = GetClockFormatter ( _24HourClock ) ;
28-
29- /// <summary>
30- /// Gets a DateTimeFormatter for the specified clock format.
31- /// </summary>
32- /// <param name="clock">The clock format ("12HourClock" or "24HourClock").</param>
33- /// <returns>A DateTimeFormatter for the specified clock format.</returns>
34- private static DateTimeFormatter GetClockFormatter ( string clock )
35- {
36- var languages = GlobalizationPreferences . Languages ;
37- var geographicRegion = GlobalizationPreferences . HomeGeographicRegion ;
38- var calendar = GlobalizationPreferences . Calendars [ 0 ] ;
39-
40- return new DateTimeFormatter ( "shorttime" , languages , geographicRegion , calendar , clock ) ;
41- }
20+ private static readonly Dictionary < ( string Format , string ? Clock ) , DateTimeFormatter > _formatters = [ ] ;
4221
4322 /// <summary>
4423 /// Sets the formatted text for a TextBlock based on its value and format.
@@ -53,7 +32,7 @@ private static void SetFormattedText(TextBlock textBlock)
5332 {
5433 if ( value is not null && format is _12HourClock or _24HourClock )
5534 {
56- var formatter = format is _24HourClock ? _24HourClockFormatter : _12HourClockFormatter ;
35+ var formatter = GetDateTimeFormatter ( "shorttime" , format ) ;
5736 var dateTimeOffset = value switch
5837 {
5938 TimeSpan timeSpan => timeSpan . ToDateTimeOffset ( ) ,
@@ -67,7 +46,8 @@ private static void SetFormattedText(TextBlock textBlock)
6746 }
6847 else if ( value is not null )
6948 {
70- var formatter = new DateTimeFormatter ( format ) ;
49+ var formatter = GetDateTimeFormatter ( format ) ;
50+
7151 var dateTimeOffset = value switch
7252 {
7353 DateOnly dateOnly => dateOnly . ToDateTimeOffset ( ) ,
@@ -90,6 +70,29 @@ private static void SetFormattedText(TextBlock textBlock)
9070 }
9171 }
9272
73+ /// <summary>
74+ /// Gets a DateTimeFormatter for the specified format and clock.
75+ /// </summary>
76+ internal static DateTimeFormatter GetDateTimeFormatter ( string strFormat , string ? strClock = null )
77+ {
78+ if ( _formatters . TryGetValue ( ( strFormat , strClock ) , out var cacheFormatter ) )
79+ {
80+ return cacheFormatter ;
81+ }
82+
83+ var formatter = new DateTimeFormatter ( strFormat ) ;
84+ var result = new DateTimeFormatter (
85+ strFormat ,
86+ formatter . Languages ,
87+ formatter . GeographicRegion ,
88+ formatter . Calendar ,
89+ strClock ?? formatter . Clock ) ;
90+
91+ _formatters [ ( strFormat , strClock ) ] = result ;
92+
93+ return result ;
94+ }
95+
9396 /// <summary>
9497 /// Handles changes to the Value attached property.
9598 /// </summary>
0 commit comments