mirror of
https://github.com/JogmansonReal/Radicade.git
synced 2026-10-25 23:18:56 -07:00
33 lines
1.1 KiB
C#
33 lines
1.1 KiB
C#
using System.Globalization;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
|
|
namespace RadicadeServer.RecNet.Events
|
|
{
|
|
public static class EventClock
|
|
{
|
|
public static DateTime FromPostedTime(DateTime posted) => posted.Kind switch
|
|
{
|
|
DateTimeKind.Utc => posted,
|
|
DateTimeKind.Local => posted.ToUniversalTime(),
|
|
_ => TimeZoneInfo.ConvertTimeToUtc(posted, TimeZoneInfo.Local)
|
|
};
|
|
|
|
public static string ToWire(DateTime stored) => (stored.Kind == DateTimeKind.Local
|
|
? stored.ToUniversalTime()
|
|
: stored)
|
|
.ToString("yyyy-MM-dd'T'HH:mm:ss.fffffff'Z'", CultureInfo.InvariantCulture);
|
|
|
|
public sealed class Json : JsonConverter<DateTime>
|
|
{
|
|
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
|
=> reader.GetString() is { } raw
|
|
? DateTime.Parse(raw, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind)
|
|
: default;
|
|
|
|
public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
|
|
=> writer.WriteStringValue(ToWire(value));
|
|
}
|
|
}
|
|
}
|