19 lines
653 B
C#
19 lines
653 B
C#
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
|
|
sealed class UnixEpochDateTimeConverter : JsonConverter<DateTime>
|
|
{
|
|
static readonly DateTime s_epoch = new(1970, 1, 1, 0, 0, 0);
|
|
|
|
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
|
{
|
|
long unixTime = reader.GetInt64();
|
|
return s_epoch.AddMilliseconds(unixTime);
|
|
}
|
|
|
|
public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
|
|
{
|
|
long unixTime = Convert.ToInt64((value - s_epoch).TotalMilliseconds);
|
|
writer.WriteNumberValue(unixTime);
|
|
}
|
|
} |