Browse Source

Refactor date time format examples

Daniel Chalmers 1 year ago
parent
commit
b0840524b2
3 changed files with 49 additions and 33 deletions
  1. 46 0
      DesktopClock/DateFormatExample.cs
  2. 0 30
      DesktopClock/DateTimeUtil.cs
  3. 3 3
      DesktopClock/MainWindow.xaml

+ 46 - 0
DesktopClock/DateFormatExample.cs

@@ -0,0 +1,46 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+
+namespace DesktopClock;
+
+public record DateFormatExample
+{
+    private DateFormatExample(string format, string example)
+    {
+        Format = format;
+        Example = example;
+    }
+
+    public string Format { get; }
+    public string Example { get; }
+
+    /// <summary>
+    /// Creates a <see cref="DateFormatExample" /> from the given format.
+    /// </summary>
+    public static DateFormatExample FromFormat(string format) => new(format, DateTimeOffset.Now.ToString(format));
+
+    /// <summary>
+    /// Common date time formatting strings and an example string for each.
+    /// </summary>
+    /// <remarks>https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings</remarks>
+    public static IReadOnlyCollection<DateFormatExample> DefaultExamples { get; } = new[]
+    {
+        "M",
+        "dddd, MMMM dd",
+        "dddd, MMMM dd, HH:mm",
+        "dddd, MMM dd, HH:mm",
+        "dddd, MMM dd, HH:mm:ss",
+        "ddd, MMMM dd, HH:mm",
+        "ddd, MMMM dd, HH:mm:ss",
+        "ddd, MMM dd, HH:mm",
+        "ddd, MMM dd, HH:mm:ss",
+        "ddd, MMM dd, HH:mm K",
+        "d",
+        "g",
+        "G",
+        "t",
+        "T",
+        "O",
+    }.Select(FromFormat).ToList();
+}

+ 0 - 30
DesktopClock/DateTimeUtil.cs

@@ -1,40 +1,10 @@
 using System;
 using System.Collections.Generic;
-using System.Linq;
 
 namespace DesktopClock;
 
 public static class DateTimeUtil
 {
-    /// <summary>
-    /// A collection of DateTime formatting strings.
-    /// </summary>
-    public static IReadOnlyList<string> DateTimeFormats { get; } = new[]
-    {
-        "M",
-        "dddd, MMMM dd",
-        "dddd, MMMM dd, HH:mm",
-        "dddd, MMM dd, HH:mm",
-        "dddd, MMM dd, HH:mm:ss",
-        "ddd, MMMM dd, HH:mm",
-        "ddd, MMMM dd, HH:mm:ss",
-        "ddd, MMM dd, HH:mm",
-        "ddd, MMM dd, HH:mm:ss",
-        "ddd, MMM dd, HH:mm K",
-        "d",
-        "g",
-        "G",
-        "t",
-        "T",
-        "O",
-    };
-
-    /// <summary>
-    /// Common date time formatting strings and an example string for each.
-    /// </summary>
-    public static IReadOnlyDictionary<string, string> DateTimeFormatsAndExamples { get; } =
-        DateTimeFormats.ToDictionary(f => f, DateTimeOffset.Now.ToString);
-
     public static IReadOnlyCollection<TimeZoneInfo> TimeZones { get; } = TimeZoneInfo.GetSystemTimeZones();
 
     public static bool TryGetTimeZoneById(string timeZoneId, out TimeZoneInfo timeZoneInfo)

+ 3 - 3
DesktopClock/MainWindow.xaml

@@ -88,14 +88,14 @@
 				</MenuItem.Resources>
 			</MenuItem>
 
-			<MenuItem Header="_Format" ItemsSource="{x:Static local:DateTimeUtil.DateTimeFormatsAndExamples}">
+			<MenuItem Header="_Format" ItemsSource="{x:Static local:DateFormatExample.DefaultExamples}">
 				<MenuItem.Resources>
 					<Style TargetType="MenuItem">
 						<Setter Property="Command" Value="{Binding DataContext.SetFormatCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" />
 
-						<Setter Property="CommandParameter" Value="{Binding Key}" />
+						<Setter Property="CommandParameter" Value="{Binding Format}" />
 
-						<Setter Property="DisplayMemberPath" Value="Value" />
+						<Setter Property="DisplayMemberPath" Value="Example" />
 					</Style>
 				</MenuItem.Resources>
 			</MenuItem>