AutoCatAvalonia/AutoCat/Extensions/DateConverter.cs

29 lines
867 B
C#

using System;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using Avalonia.Data.Converters;
namespace AutoCat.Extensions;
public class DateConverter: IValueConverter
{
private static readonly DateTime Epoch = new(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
if (value is not double timestamp)
return null;
if (timestamp == 0)
return null;
var dateTime = Epoch.AddMilliseconds(timestamp).ToLocalTime();
return dateTime.ToString("g");
}
[SuppressMessage("ReSharper", "ReturnTypeCanBeNotNullable")]
public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}