// Copyright (c) 2017 schick Informatik // Description: Converter für die Darstellung von Databinding Values aus der DB in Local Time in der Anwendung // using System; using System.Globalization; using System.Windows.Data; using System.Windows.Markup; namespace ENI2.Util { public class UtcToLocalDateTimeConverter : MarkupExtension, IValueConverter { private static UtcToLocalDateTimeConverter _converter = null; public override object ProvideValue(IServiceProvider serviceProvider) { if (_converter == null) { _converter = new UtcToLocalDateTimeConverter(); } return _converter; } public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { DateTime? sourceVal = (DateTime?) value; if (sourceVal.HasValue) { //if(sourceVal.Value.) return DateTime.SpecifyKind(sourceVal.Value, DateTimeKind.Utc).ToLocalTime(); } else return null; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { if(value is string dateString) { if(DateTime.TryParse(dateString, out DateTime theValue)) { return DateTime.SpecifyKind(theValue, DateTimeKind.Local).ToUniversalTime(); } else { return null; } } else if(value is DateTime?) { DateTime? sourceVal = (DateTime?)value; if (sourceVal.HasValue) return DateTime.SpecifyKind(sourceVal.Value, DateTimeKind.Local).ToUniversalTime(); else return null; } else { return null; } } } }