|
1 | 1 | usingSystem; |
2 | 2 | usingSystem.Globalization; |
| 3 | +usingSystem.Windows; |
3 | 4 | usingSystem.Windows.Data; |
4 | 5 |
|
5 | 6 | namespaceUnityLauncherPro.Converters |
6 | 7 | { |
7 | | -[ValueConversion(typeof(DateTime),typeof(String))] |
| 8 | +[ValueConversion(typeof(DateTime),typeof(string))] |
8 | 9 | publicclassReleaseDateConverter:IValueConverter |
9 | 10 | { |
10 | 11 | publicobjectConvert(objectvalue,TypetargetType,objectparameter,CultureInfoculture) |
11 | 12 | { |
12 | | -if(value==null)returnnull; |
13 | | -DateTimedate=(DateTime)value; |
| 13 | +if(value==null||!(valueisDateTimedate)) |
| 14 | +{ |
| 15 | +returnDependencyProperty.UnsetValue; |
| 16 | +} |
14 | 17 |
|
15 | | -// get first part of string until space character (updates only contain mm/dd/yyyy) |
16 | | -stringdateStrTrimmed=MainWindow.currentDateFormat; |
17 | | -if(dateStrTrimmed.IndexOf(' ')>-1)dateStrTrimmed=dateStrTrimmed.Split(' ')[0]; |
| 18 | +// Use a default date format if currentDateFormat is null or empty |
| 19 | +stringdateStrTrimmed=MainWindow.currentDateFormat??"MM/dd/yyyy"; |
18 | 20 |
|
19 | | -returnMainWindow.useHumanFriendlyDateFormat?Tools.GetElapsedTime(date):date.ToString(dateStrTrimmed); |
| 21 | +// If the format includes time, use only the date portion |
| 22 | +if(dateStrTrimmed.Contains(" ")) |
| 23 | +{ |
| 24 | +dateStrTrimmed=dateStrTrimmed.Split(' ')[0]; |
| 25 | +} |
| 26 | + |
| 27 | +// Return a human-friendly format if enabled; otherwise, format based on dateStrTrimmed |
| 28 | +returnMainWindow.useHumanFriendlyDateFormat |
| 29 | +?Tools.GetElapsedTime(date) |
| 30 | +:date.ToString(dateStrTrimmed,culture); |
20 | 31 | } |
21 | 32 |
|
22 | 33 | publicobjectConvertBack(objectvalue,TypetargetType,objectparameter,CultureInfoculture) |
23 | 34 | { |
24 | | -// not used ? |
25 | | -returnDateTime.ParseExact((string)value,MainWindow.currentDateFormat,culture); |
26 | | -} |
| 35 | +if(value==null||string.IsNullOrWhiteSpace(value.ToString())) |
| 36 | +{ |
| 37 | +returnDependencyProperty.UnsetValue; |
| 38 | +} |
27 | 39 |
|
| 40 | +// Attempt to parse back to DateTime using the specified format |
| 41 | +if(DateTime.TryParseExact((string)value,MainWindow.currentDateFormat??"MM/dd/yyyy",culture,DateTimeStyles.None,outDateTimeparsedDate)) |
| 42 | +{ |
| 43 | +returnparsedDate; |
| 44 | +} |
| 45 | + |
| 46 | +returnDependencyProperty.UnsetValue; |
| 47 | +} |
28 | 48 | } |
29 | 49 | } |