Browse Source

Added DateTimeConverter

pull/50/head
Eris 3 years ago
parent
commit
1c5b558258
1 changed files with 23 additions and 1 deletions
  1. +23
    -1
      IPA.Loader/Config/Stores/Converters.cs

+ 23
- 1
IPA.Loader/Config/Stores/Converters.cs View File

@ -112,7 +112,8 @@ namespace IPA.Config.Stores.Converters
IValConv<short>, IValConv<ushort>,
IValConv<sbyte>, IValConv<byte>,
IValConv<float>, IValConv<double>,
IValConv<decimal>, IValConv<bool>
IValConv<decimal>, IValConv<bool>,
IValConv<DateTime>
{
internal static readonly ValConvImpls Impl = new ValConvImpls();
Type IValConv<char>.Get() => typeof(CharConverter);
@ -130,6 +131,7 @@ namespace IPA.Config.Stores.Converters
Type IValConv<double>.Get() => typeof(DoubleConverter);
Type IValConv<decimal>.Get() => typeof(DecimalConverter);
Type IValConv<bool>.Get() => typeof(BooleanConverter);
Type IValConv<DateTime>.Get() => typeof(DateTimeConverter);
}
}
@ -618,4 +620,24 @@ namespace IPA.Config.Stores.Converters
public override Value ToValue(bool obj, object parent)
=> Value.From(obj);
}
internal class DateTimeConverter : ValueConverter<DateTime>
{
public override DateTime FromValue(Value value, object parent)
{
if (!(value is Text text))
{
throw new ArgumentException("Value is not of type Text", nameof(value));
}
if (DateTime.TryParse(text.Value, out var dateTime))
{
return dateTime;
}
throw new ArgumentException($"Parsing failed, {text.Value}");
}
public override Value ToValue(DateTime obj, object parent) => Value.Text(obj.ToString("O"));
}
}

Loading…
Cancel
Save