11using System ;
22using System . Diagnostics . CodeAnalysis ;
33using System . Globalization ;
4- using DIPS . Xamarin . UI . Extensions ;
54using DIPS . Xamarin . UI . Internal . Utilities ;
6- using DIPS . Xamarin . UI . Resources . LocalizedStrings ;
75using Xamarin . Forms ;
86using Xamarin . Forms . Xaml ;
97
@@ -13,7 +11,8 @@ namespace DIPS.Xamarin.UI.Converters.ValueConverters
1311 /// Converts an DateTime object to a format and convert it to a readable string in local timezone
1412 /// </summary>
1513 public class DateConverter : IValueConverter , IMarkupExtension
16- {
14+ {
15+
1716 /// <summary>
1817 /// The formats to choose between during conversion
1918 /// </summary>
@@ -39,15 +38,13 @@ public enum DateConverterFormat
3938 /// </remarks>
4039 Text ,
4140 }
42-
43- private const string Space = " " ;
4441 private IServiceProvider m_serviceProvider ;
45-
42+
4643 /// <summary>
47- /// The format to choose between, see <see cref="DateConverterFormat" />
44+ ///
4845 /// </summary>
4946 public DateConverterFormat Format { get ; set ; }
50-
47+
5148 /// <summary>
5249 /// Ignores the conversion to the local timezone
5350 /// </summary>
@@ -64,108 +61,24 @@ public object ProvideValue(IServiceProvider serviceProvider)
6461 /// <inheritdoc />
6562 public object Convert ( object value , Type targetType , object parameter , CultureInfo culture )
6663 {
67- if ( value == null ) return string . Empty ;
68- if ( ! ( value is DateTime dateTimeInput ) )
69- throw new XamlParseException ( "The input has to be of type DateTime" ) . WithXmlLineInfo ( m_serviceProvider ) ;
70- return Format switch
71- {
72- DateConverterFormat . Short => ConvertToDefaultDateTime ( dateTimeInput , culture , IgnoreLocalTime ) ,
73- DateConverterFormat . Text =>
74- ConvertDateTimeAsText ( dateTimeInput , culture , IgnoreLocalTime ) ,
75- _ => string . Empty
76- } ;
77- }
78-
79- /// <inheritdoc />
80- [ ExcludeFromCodeCoverage ]
81- public object ConvertBack ( object value , Type targetType , object parameter , CultureInfo culture )
82- {
83- throw new NotImplementedException ( ) ;
84- }
85-
86- private static string ConvertToDefaultDateTime ( DateTime dateTime , CultureInfo culture , bool ignoreLocalTime )
87- {
88- if ( ! ignoreLocalTime )
89- {
90- dateTime = dateTime . ToLocalTime ( ) ;
91- }
92-
93- var day = GetDayBasedOnCulture ( dateTime , culture ) ;
94-
95- var month = GetMonthBasedOnCulture ( dateTime , culture ) ;
96- var year = dateTime . ToString ( "yyyy" , culture ) ;
97- if ( culture . ThreeLetterWindowsLanguageName . Equals ( "ENU" ) )
98- {
99- return $ "{ month } { Space } { day } { Space } { year } ";
100- }
101-
102- return $ "{ day } { Space } { month } { Space } { year } ";
103- }
104-
105- private static string GetDayBasedOnCulture ( DateTime dateTime , CultureInfo culture )
106- {
107- var day = dateTime . ToString ( "dd" , culture ) ;
108- if ( culture . TwoLetterISOLanguageName . Contains ( "en" ) )
109- {
110- day = day . TrimStart ( '0' ) ;
111- day += dateTime . GetEnglishDaySuffix ( ) ;
112- }
113-
114- if ( culture . ThreeLetterWindowsLanguageName . Equals ( "ENU" ) )
115- {
116- day += "," ;
117- }
118-
119- if ( culture . IsNorwegian ( ) )
120- {
121- day += "." ;
122- }
123-
124- return day ;
125- }
126-
127- private static string ConvertDateTimeAsText ( DateTime dateTime , CultureInfo culture , bool ignoreLocalTime )
128- {
129- if ( ! ignoreLocalTime )
64+ if ( value == null )
13065 {
131- dateTime = dateTime . ToLocalTime ( ) ;
132- }
133-
134- if ( dateTime . IsToday ( ) )
135- {
136- return InternalLocalizedStrings . Today ;
66+ return string . Empty ;
13767 }
13868
139- if ( dateTime . IsYesterday ( ) )
69+ if ( value is not DateTime dateTimeInput )
14070 {
141- return InternalLocalizedStrings . Yesterday ;
142- }
143-
144- if ( dateTime . IsTomorrow ( ) )
145- {
146- return InternalLocalizedStrings . Tomorrow ;
147- }
148-
149- var month = GetMonthBasedOnCulture ( dateTime , culture ) ;
150- var day = GetDayBasedOnCulture ( dateTime , culture ) ;
151-
152- if ( culture . ThreeLetterWindowsLanguageName . Equals ( "ENU" ) )
153- {
154- return $ "{ month } { Space } { day } ";
71+ throw new XamlParseException ( "The input has to be of type DateTime" ) . WithXmlLineInfo ( m_serviceProvider ) ;
15572 }
15673
157- return $ " { day } { Space } { month } " ;
74+ return DateTimeFormatter . FormatDate ( dateTimeInput , culture , IgnoreLocalTime , Format ) ;
15875 }
15976
160- private static string GetMonthBasedOnCulture ( DateTime dateTime , CultureInfo culture )
77+ /// <inheritdoc />
78+ [ ExcludeFromCodeCoverage ]
79+ public object ConvertBack ( object value , Type targetType , object parameter , CultureInfo culture )
16180 {
162- var month = dateTime . ToString ( "MMM" , culture ) ;
163- if ( culture . TwoLetterISOLanguageName . Contains ( "en" ) )
164- {
165- month = month [ 0 ] . ToString ( ) . ToUpper ( ) + month . Substring ( 1 ) ;
166- }
167-
168- return month ;
81+ throw new NotImplementedException ( ) ;
16982 }
17083 }
17184}
0 commit comments