From 9a383d2bd44e61e5427fe9e7ca02dba14c3a498e Mon Sep 17 00:00:00 2001 From: Alexey Golub Date: Thu, 6 Jun 2019 23:31:40 +0300 Subject: [PATCH] Convert from DateTime to DateTimeOffset instead of parsing it directly Json.NET is really not meant to be used with DateTimeOffset it seems Fixes #179 --- DiscordChatExporter.Core.Services/DataService.Parsers.cs | 6 +++--- DiscordChatExporter.Core.Services/DataService.cs | 9 +-------- DiscordChatExporter.Core.Services/Internal/Extensions.cs | 8 ++++---- 3 files changed, 8 insertions(+), 15 deletions(-) diff --git a/DiscordChatExporter.Core.Services/DataService.Parsers.cs b/DiscordChatExporter.Core.Services/DataService.Parsers.cs index d008c119..3207f5ce 100644 --- a/DiscordChatExporter.Core.Services/DataService.Parsers.cs +++ b/DiscordChatExporter.Core.Services/DataService.Parsers.cs @@ -118,7 +118,7 @@ namespace DiscordChatExporter.Core.Services var title = json["title"]?.Value(); var description = json["description"]?.Value(); var url = json["url"]?.Value(); - var timestamp = json["timestamp"]?.Value(); + var timestamp = json["timestamp"]?.Value().ToDateTimeOffset(); // Get color var color = json["color"] != null @@ -165,8 +165,8 @@ namespace DiscordChatExporter.Core.Services // Get basic data var id = json["id"].Value(); var channelId = json["channel_id"].Value(); - var timestamp = json["timestamp"].Value(); - var editedTimestamp = json["edited_timestamp"]?.Value(); + var timestamp = json["timestamp"].Value().ToDateTimeOffset(); + var editedTimestamp = json["edited_timestamp"]?.Value()?.ToDateTimeOffset(); var content = json["content"].Value(); var type = (MessageType) json["type"].Value(); diff --git a/DiscordChatExporter.Core.Services/DataService.cs b/DiscordChatExporter.Core.Services/DataService.cs index c6d0f3b2..6f0010d9 100644 --- a/DiscordChatExporter.Core.Services/DataService.cs +++ b/DiscordChatExporter.Core.Services/DataService.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.IO; using System.Linq; using System.Net.Http; using System.Net.Http.Headers; @@ -9,7 +8,6 @@ using DiscordChatExporter.Core.Models; using DiscordChatExporter.Core.Services.Exceptions; using DiscordChatExporter.Core.Services.Internal; using Failsafe; -using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Tyrrrz.Extensions; @@ -66,12 +64,7 @@ namespace DiscordChatExporter.Core.Services var raw = await response.Content.ReadAsStringAsync(); // Parse - using (var reader = new JsonTextReader(new StringReader(raw))) - { - reader.DateParseHandling = DateParseHandling.DateTimeOffset; - - return JToken.Load(reader); - } + return JToken.Parse(raw); } } }); diff --git a/DiscordChatExporter.Core.Services/Internal/Extensions.cs b/DiscordChatExporter.Core.Services/Internal/Extensions.cs index c722bc60..c5d9ec65 100644 --- a/DiscordChatExporter.Core.Services/Internal/Extensions.cs +++ b/DiscordChatExporter.Core.Services/Internal/Extensions.cs @@ -5,11 +5,11 @@ namespace DiscordChatExporter.Core.Services.Internal { internal static class Extensions { - public static string ToSnowflake(this DateTimeOffset date) + public static DateTimeOffset ToDateTimeOffset(this DateTime dateTime) => new DateTimeOffset(dateTime); + + public static string ToSnowflake(this DateTimeOffset dateTime) { - const long epoch = 62135596800000; - var unixTime = date.ToUniversalTime().Ticks / TimeSpan.TicksPerMillisecond - epoch; - var value = ((ulong) unixTime - 1420070400000UL) << 22; + var value = ((ulong) dateTime.ToUnixTimeMilliseconds() - 1420070400000UL) << 22; return value.ToString(); }