aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/SideChains/MaterialDesignInXamlToolkit-master/MaterialDesignThemes.Wpf/DateTimeEx.cs
blob: b814739d78d2b90adfad4ea4f0ef2c771f05f7a8 (plain)
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)
			{
				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;
		}
	}
}