From 134ee850673ecc18eff98b854b9aaf130d62d2b9 Mon Sep 17 00:00:00 2001 From: Chris Cameron Date: Sun, 31 Mar 2019 20:38:22 -0400 Subject: [PATCH] feat: Adding JsonReader extension to read current value as DateTime --- .../Extensions/JsonReaderExtensions.cs | 15 +++++++++++++++ ICD.Common.Utils/Json/JsonUtils.cs | 12 +++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/ICD.Common.Utils/Extensions/JsonReaderExtensions.cs b/ICD.Common.Utils/Extensions/JsonReaderExtensions.cs index 5c4a023..6fd875c 100644 --- a/ICD.Common.Utils/Extensions/JsonReaderExtensions.cs +++ b/ICD.Common.Utils/Extensions/JsonReaderExtensions.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using ICD.Common.Properties; +using ICD.Common.Utils.Json; using Newtonsoft.Json; namespace ICD.Common.Utils.Extensions @@ -205,6 +206,20 @@ namespace ICD.Common.Utils.Extensions return new Guid(stringValue); } + /// + /// Gets the current value as a date. + /// + /// + /// + public static DateTime GetValueAsDateTime(this JsonReader extends) + { + if (extends == null) + throw new ArgumentNullException("extends"); + + string stringValue = extends.GetValueAsString(); + return JsonUtils.ParseDateTime(stringValue); + } + /// /// Deserializes an array of items from the reader's current value. /// diff --git a/ICD.Common.Utils/Json/JsonUtils.cs b/ICD.Common.Utils/Json/JsonUtils.cs index 870bc56..afdd0c3 100644 --- a/ICD.Common.Utils/Json/JsonUtils.cs +++ b/ICD.Common.Utils/Json/JsonUtils.cs @@ -22,6 +22,16 @@ namespace ICD.Common.Utils.Json private const string MESSAGE_NAME_PROPERTY = "m"; private const string MESSAGE_DATA_PROPERTY = "d"; + /// + /// Gets the data as a DateTime value. + /// + /// + /// + public static DateTime ParseDateTime(string data) + { + return DateTime.ParseExact(data, DATE_FORMAT, CultureInfo.CurrentCulture); + } + /// /// Gets the token as a DateTime value. /// @@ -34,7 +44,7 @@ namespace ICD.Common.Utils.Json throw new ArgumentNullException("token"); #if SIMPLSHARP - return DateTime.ParseExact((string)token, DATE_FORMAT, CultureInfo.CurrentCulture); + return ParseDateTime((string)token); #else return (DateTime)token; #endif