99namespace DIPS . Xamarin . UI . Converters . ValueConverters
1010{
1111 /// <summary>
12- /// Converters a DateTime object with an format and convert it to a readable string
12+ /// Converters a DateTime object with a format and convert it to a readable string in local time zone
1313 /// </summary>
1414 public class DateAndTimeConverter : IMarkupExtension , IValueConverter
1515 {
@@ -36,6 +36,11 @@ public enum DateAndTimeConverterFormat
3636 Text ,
3737 }
3838
39+ /// <summary>
40+ /// Ignores the conversion to local timezone
41+ /// </summary>
42+ public bool IgnoreLocalTime { get ; set ; } = false ;
43+
3944 private const string Space = " " ;
4045 private IServiceProvider m_serviceProvider ;
4146
@@ -60,9 +65,9 @@ public object Convert(object value, Type targetType, object parameter, CultureIn
6065 throw new XamlParseException ( "The input has to be of type DateTime" ) . WithXmlLineInfo ( m_serviceProvider ) ;
6166 return Format switch
6267 {
63- DateAndTimeConverterFormat . Short => ConvertToShortFormat ( dateTimeInput , culture ) ,
68+ DateAndTimeConverterFormat . Short => ConvertToShortFormat ( dateTimeInput , culture , IgnoreLocalTime ) ,
6469 DateAndTimeConverterFormat . Text
65- => ConvertToTextFormat ( dateTimeInput , culture ) ,
70+ => ConvertToTextFormat ( dateTimeInput , culture , IgnoreLocalTime ) ,
6671 _ => string . Empty
6772 } ;
6873 }
@@ -74,11 +79,11 @@ public object ConvertBack(object value, Type targetType, object parameter, Cultu
7479 throw new NotImplementedException ( ) ;
7580 }
7681
77- private static string ConvertToTextFormat ( DateTime dateTimeInput , CultureInfo culture )
82+ private static string ConvertToTextFormat ( DateTime dateTimeInput , CultureInfo culture , bool ignoreLocalTime )
7883 {
79- var date = new DateConverter { Format = DateConverter . DateConverterFormat . Text } . Convert ( dateTimeInput , null ,
84+ var date = new DateConverter { Format = DateConverter . DateConverterFormat . Text , IgnoreLocalTime = ignoreLocalTime } . Convert ( dateTimeInput , null ,
8085 null , culture ) ;
81- var time = new TimeConverter { Format = TimeConverter . TimeConverterFormat . Default } . Convert ( dateTimeInput ,
86+ var time = new TimeConverter { Format = TimeConverter . TimeConverterFormat . Default , IgnoreLocalTime = ignoreLocalTime } . Convert ( dateTimeInput ,
8287 null , null , culture ) ;
8388
8489 if ( culture . IsNorwegian ( ) )
@@ -94,11 +99,11 @@ private static string ConvertToTextFormat(DateTime dateTimeInput, CultureInfo cu
9499 return $ "{ date } { Space } { time } ";
95100 }
96101
97- private static string ConvertToShortFormat ( DateTime dateTimeInput , CultureInfo culture )
102+ private static string ConvertToShortFormat ( DateTime dateTimeInput , CultureInfo culture , bool ignoreLocalTime )
98103 {
99- var date = new DateConverter { Format = DateConverter . DateConverterFormat . Short } . Convert ( dateTimeInput , null ,
104+ var date = new DateConverter { Format = DateConverter . DateConverterFormat . Short , IgnoreLocalTime = ignoreLocalTime } . Convert ( dateTimeInput , null ,
100105 null , culture ) ;
101- var time = new TimeConverter { Format = TimeConverter . TimeConverterFormat . Default } . Convert ( dateTimeInput ,
106+ var time = new TimeConverter { Format = TimeConverter . TimeConverterFormat . Default , IgnoreLocalTime = ignoreLocalTime } . Convert ( dateTimeInput ,
102107 null , null , culture ) ;
103108
104109 return culture . IsNorwegian ( ) ? $ "{ date } { Space } kl{ Space } { time } " : $ "{ date } { Space } { time } ";
0 commit comments