1
1
using Microsoft . UI . Xaml ;
2
2
using Microsoft . UI . Xaml . Controls ;
3
3
using System ;
4
+ using System . Collections . Generic ;
4
5
using System . Diagnostics ;
6
+ using System . Globalization ;
5
7
using Windows . Globalization . DateTimeFormatting ;
6
8
using Windows . System . UserProfile ;
7
9
using WinUI . TableView . Extensions ;
8
10
9
11
namespace WinUI . TableView . Helpers ;
10
12
11
13
/// <summary>
12
- /// Provides helper methods for formatting Date& Time values.
14
+ /// Provides helper methods for formatting Date and Time values.
13
15
/// </summary>
14
16
internal static class DateTimeFormatHelper
15
17
{
16
18
private const string _12HourClock = "12HourClock" ;
17
19
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 = [ ] ;
42
21
43
22
/// <summary>
44
23
/// Sets the formatted text for a TextBlock based on its value and format.
@@ -53,7 +32,7 @@ private static void SetFormattedText(TextBlock textBlock)
53
32
{
54
33
if ( value is not null && format is _12HourClock or _24HourClock )
55
34
{
56
- var formatter = format is _24HourClock ? _24HourClockFormatter : _12HourClockFormatter ;
35
+ var formatter = GetDateTimeFormatter ( "shorttime" , format ) ;
57
36
var dateTimeOffset = value switch
58
37
{
59
38
TimeSpan timeSpan => timeSpan . ToDateTimeOffset ( ) ,
@@ -67,7 +46,8 @@ private static void SetFormattedText(TextBlock textBlock)
67
46
}
68
47
else if ( value is not null )
69
48
{
70
- var formatter = new DateTimeFormatter ( format ) ;
49
+ var formatter = GetDateTimeFormatter ( format ) ;
50
+
71
51
var dateTimeOffset = value switch
72
52
{
73
53
DateOnly dateOnly => dateOnly . ToDateTimeOffset ( ) ,
@@ -90,6 +70,29 @@ private static void SetFormattedText(TextBlock textBlock)
90
70
}
91
71
}
92
72
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
+
93
96
/// <summary>
94
97
/// Handles changes to the Value attached property.
95
98
/// </summary>
0 commit comments