feat: Adding JsonReader extension to read current value as DateTime

This commit is contained in:
Chris Cameron
2019-03-31 20:38:22 -04:00
parent 24601b0ef2
commit 134ee85067
2 changed files with 26 additions and 1 deletions

View File

@@ -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);
}
/// <summary>
/// Gets the current value as a date.
/// </summary>
/// <param name="extends"></param>
/// <returns></returns>
public static DateTime GetValueAsDateTime(this JsonReader extends)
{
if (extends == null)
throw new ArgumentNullException("extends");
string stringValue = extends.GetValueAsString();
return JsonUtils.ParseDateTime(stringValue);
}
/// <summary>
/// Deserializes an array of items from the reader's current value.
/// </summary>

View File

@@ -22,6 +22,16 @@ namespace ICD.Common.Utils.Json
private const string MESSAGE_NAME_PROPERTY = "m";
private const string MESSAGE_DATA_PROPERTY = "d";
/// <summary>
/// Gets the data as a DateTime value.
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static DateTime ParseDateTime(string data)
{
return DateTime.ParseExact(data, DATE_FORMAT, CultureInfo.CurrentCulture);
}
/// <summary>
/// Gets the token as a DateTime value.
/// </summary>
@@ -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