From ddd96fc59705f328fee5a1ba6b861e096a93fd10 Mon Sep 17 00:00:00 2001 From: Eris Date: Fri, 28 Aug 2020 17:39:18 +0200 Subject: [PATCH] Added DateTimeOffsetConverter --- IPA.Loader/Config/Stores/Converters.cs | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/IPA.Loader/Config/Stores/Converters.cs b/IPA.Loader/Config/Stores/Converters.cs index 6cd104c4..f709b2a6 100644 --- a/IPA.Loader/Config/Stores/Converters.cs +++ b/IPA.Loader/Config/Stores/Converters.cs @@ -114,7 +114,7 @@ namespace IPA.Config.Stores.Converters IValConv, IValConv, IValConv, IValConv, IValConv, IValConv, - IValConv + IValConv, IValConv { internal static readonly ValConvImpls Impl = new ValConvImpls(); Type IValConv.Get() => typeof(CharConverter); @@ -133,6 +133,7 @@ namespace IPA.Config.Stores.Converters Type IValConv.Get() => typeof(DecimalConverter); Type IValConv.Get() => typeof(BooleanConverter); Type IValConv.Get() => typeof(DateTimeConverter); + Type IValConv.Get() => typeof(DateTimeOffsetConverter); } } @@ -657,7 +658,7 @@ namespace IPA.Config.Stores.Converters public override Value ToValue(bool obj, object parent) => Value.From(obj); } - + internal class DateTimeConverter : ValueConverter { public override DateTime FromValue(Value value, object parent) @@ -677,4 +678,24 @@ namespace IPA.Config.Stores.Converters public override Value ToValue(DateTime obj, object parent) => Value.Text(obj.ToString("O")); } + + internal class DateTimeOffsetConverter : ValueConverter + { + 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")); + } }