1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
using System;
using System.ComponentModel;
using System.Globalization;
namespace Timeline.Models.Converters
{
public class MyDateTimeConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType)
{
return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
}
public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType)
{
return base.CanConvertTo(context, destinationType);
}
public override object? ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object value)
{
if (value is string text)
{
text = text.Trim();
if (text.Length == 0)
{
return DateTime.SpecifyKind(DateTime.MinValue, DateTimeKind.Utc);
}
return DateTime.Parse(text, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal);
}
return base.ConvertFrom(context, culture, value);
}
public override object? ConvertTo(ITypeDescriptorContext? context, CultureInfo? culture, object? value, Type destinationType)
{
if (destinationType == typeof(string) && value is DateTime)
{
DateTime dt = (DateTime)value;
if (dt == DateTime.MinValue)
{
return string.Empty;
}
return dt.ToString("s", CultureInfo.InvariantCulture) + "Z";
}
return base.ConvertTo(context, culture, value, destinationType);
}
}
}
|