Browse Source

Merge pull request #51 from ErisApps/feature/Add_DateTimeOffset_and_TimeSpan_Converters

Feature/add date time offset and time span converters
pull/53/head
Anairkoen Schno 4 years ago
committed by GitHub
parent
commit
7d39fdbb85
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 45 additions and 2 deletions
  1. +45
    -2
      IPA.Loader/Config/Stores/Converters.cs

+ 45
- 2
IPA.Loader/Config/Stores/Converters.cs View File

@ -114,7 +114,8 @@ namespace IPA.Config.Stores.Converters
IValConv<sbyte>, IValConv<byte>, IValConv<sbyte>, IValConv<byte>,
IValConv<float>, IValConv<double>, IValConv<float>, IValConv<double>,
IValConv<decimal>, IValConv<bool>, IValConv<decimal>, IValConv<bool>,
IValConv<DateTime>
IValConv<DateTime>, IValConv<DateTimeOffset>,
IValConv<TimeSpan>
{ {
internal static readonly ValConvImpls Impl = new ValConvImpls(); internal static readonly ValConvImpls Impl = new ValConvImpls();
Type IValConv<char>.Get() => typeof(CharConverter); Type IValConv<char>.Get() => typeof(CharConverter);
@ -133,6 +134,8 @@ namespace IPA.Config.Stores.Converters
Type IValConv<decimal>.Get() => typeof(DecimalConverter); Type IValConv<decimal>.Get() => typeof(DecimalConverter);
Type IValConv<bool>.Get() => typeof(BooleanConverter); Type IValConv<bool>.Get() => typeof(BooleanConverter);
Type IValConv<DateTime>.Get() => typeof(DateTimeConverter); Type IValConv<DateTime>.Get() => typeof(DateTimeConverter);
Type IValConv<DateTimeOffset>.Get() => typeof(DateTimeOffsetConverter);
Type IValConv<TimeSpan>.Get() => typeof(TimeSpanConverter);
} }
} }
@ -657,7 +660,7 @@ namespace IPA.Config.Stores.Converters
public override Value ToValue(bool obj, object parent) public override Value ToValue(bool obj, object parent)
=> Value.From(obj); => Value.From(obj);
} }
internal class DateTimeConverter : ValueConverter<DateTime> internal class DateTimeConverter : ValueConverter<DateTime>
{ {
public override DateTime FromValue(Value value, object parent) public override DateTime FromValue(Value value, object parent)
@ -677,4 +680,44 @@ namespace IPA.Config.Stores.Converters
public override Value ToValue(DateTime obj, object parent) => Value.Text(obj.ToString("O")); public override Value ToValue(DateTime obj, object parent) => Value.Text(obj.ToString("O"));
} }
internal class DateTimeOffsetConverter : ValueConverter<DateTimeOffset>
{
public override DateTimeOffset FromValue(Value value, object parent)
{
if (!(value is Text text))
{
throw new ArgumentException("Value is not of type Text", nameof(value));
}
if (DateTimeOffset.TryParse(text.Value, out var dateTime))
{
return dateTime;
}
throw new ArgumentException($"Parsing failed, {text.Value}");
}
public override Value ToValue(DateTimeOffset obj, object parent) => Value.Text(obj.ToString("O"));
}
internal class TimeSpanConverter : ValueConverter<TimeSpan>
{
public override TimeSpan FromValue(Value value, object parent)
{
if (!(value is Text text))
{
throw new ArgumentException("Value is not of type Text", nameof(value));
}
if (TimeSpan.TryParse(text.Value, out var dateTime))
{
return dateTime;
}
throw new ArgumentException($"Parsing failed, {text.Value}");
}
public override Value ToValue(TimeSpan obj, object parent) => Value.Text(obj.ToString());
}
} }

Loading…
Cancel
Save