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
pull/196/head
Alexey Golub 6 years ago
parent 2ed374ee5c
commit 9a383d2bd4

@ -118,7 +118,7 @@ namespace DiscordChatExporter.Core.Services
var title = json["title"]?.Value<string>(); var title = json["title"]?.Value<string>();
var description = json["description"]?.Value<string>(); var description = json["description"]?.Value<string>();
var url = json["url"]?.Value<string>(); var url = json["url"]?.Value<string>();
var timestamp = json["timestamp"]?.Value<DateTimeOffset>(); var timestamp = json["timestamp"]?.Value<DateTime>().ToDateTimeOffset();
// Get color // Get color
var color = json["color"] != null var color = json["color"] != null
@ -165,8 +165,8 @@ namespace DiscordChatExporter.Core.Services
// Get basic data // Get basic data
var id = json["id"].Value<string>(); var id = json["id"].Value<string>();
var channelId = json["channel_id"].Value<string>(); var channelId = json["channel_id"].Value<string>();
var timestamp = json["timestamp"].Value<DateTimeOffset>(); var timestamp = json["timestamp"].Value<DateTime>().ToDateTimeOffset();
var editedTimestamp = json["edited_timestamp"]?.Value<DateTimeOffset?>(); var editedTimestamp = json["edited_timestamp"]?.Value<DateTime?>()?.ToDateTimeOffset();
var content = json["content"].Value<string>(); var content = json["content"].Value<string>();
var type = (MessageType) json["type"].Value<int>(); var type = (MessageType) json["type"].Value<int>();

@ -1,6 +1,5 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Linq; using System.Linq;
using System.Net.Http; using System.Net.Http;
using System.Net.Http.Headers; using System.Net.Http.Headers;
@ -9,7 +8,6 @@ using DiscordChatExporter.Core.Models;
using DiscordChatExporter.Core.Services.Exceptions; using DiscordChatExporter.Core.Services.Exceptions;
using DiscordChatExporter.Core.Services.Internal; using DiscordChatExporter.Core.Services.Internal;
using Failsafe; using Failsafe;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using Tyrrrz.Extensions; using Tyrrrz.Extensions;
@ -66,12 +64,7 @@ namespace DiscordChatExporter.Core.Services
var raw = await response.Content.ReadAsStringAsync(); var raw = await response.Content.ReadAsStringAsync();
// Parse // Parse
using (var reader = new JsonTextReader(new StringReader(raw))) return JToken.Parse(raw);
{
reader.DateParseHandling = DateParseHandling.DateTimeOffset;
return JToken.Load(reader);
}
} }
} }
}); });

@ -5,11 +5,11 @@ namespace DiscordChatExporter.Core.Services.Internal
{ {
internal static class Extensions 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 value = ((ulong) dateTime.ToUnixTimeMilliseconds() - 1420070400000UL) << 22;
var unixTime = date.ToUniversalTime().Ticks / TimeSpan.TicksPerMillisecond - epoch;
var value = ((ulong) unixTime - 1420070400000UL) << 22;
return value.ToString(); return value.ToString();
} }

Loading…
Cancel
Save