mirror of
https://github.com/JogmansonReal/Radicade.git
synced 2026-10-26 07:29:00 -07:00
Radicade Public Code
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user