forked from MaterialDesignInXAML/MaterialDesignInXamlToolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDateTimeEx.cs
More file actions
51 lines (43 loc) · 1.24 KB
/
Copy pathDateTimeEx.cs
File metadata and controls
51 lines (43 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using System;
using System.Globalization;
using System.Linq;
namespace MaterialDesignThemes.Wpf
{
internal static class DateTimeEx
{
internal static DateTimeFormatInfo GetDateFormat(this CultureInfo culture)
{
if (culture == null) throw new ArgumentNullException(nameof(culture));
if (culture.Calendar is GregorianCalendar || culture.Calendar is PersianCalendar)
{
return culture.DateTimeFormat;
}
GregorianCalendar foundCal = null;
DateTimeFormatInfo dtfi = null;
foreach (var cal in culture.OptionalCalendars.OfType<GregorianCalendar>())
{
// Return the first Gregorian calendar with CalendarType == Localized
// Otherwise return the first Gregorian calendar
if (foundCal == null)
{
foundCal = cal;
}
if (cal.CalendarType != GregorianCalendarTypes.Localized) continue;
foundCal = cal;
break;
}
if (foundCal == null)
{
// if there are no GregorianCalendars in the OptionalCalendars list, use the invariant dtfi
dtfi = ((CultureInfo)CultureInfo.InvariantCulture.Clone()).DateTimeFormat;
dtfi.Calendar = new GregorianCalendar();
}
else
{
dtfi = ((CultureInfo)culture.Clone()).DateTimeFormat;
dtfi.Calendar = foundCal;
}
return dtfi;
}
}
}