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 5 years ago
parent 2ed374ee5c
commit 9a383d2bd4

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

@ -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);
}
}
});

@ -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();
}

Loading…
Cancel
Save