From 5dd0fe6d064bb8bec66aab0987c533d2cabd5f57 Mon Sep 17 00:00:00 2001 From: cupsos Date: Thu, 22 Dec 2022 11:00:55 +0900 Subject: [PATCH] Fix #121 --- .../HTML5/EpochTimestampDateTimeConverter.cs | 21 +++++++++++++++++++ .../HTML5/HtmlNotificationData.cs | 2 ++ 2 files changed, 23 insertions(+) create mode 100644 src/Majorsoft.Blazor.Components.Notifications/HTML5/EpochTimestampDateTimeConverter.cs diff --git a/src/Majorsoft.Blazor.Components.Notifications/HTML5/EpochTimestampDateTimeConverter.cs b/src/Majorsoft.Blazor.Components.Notifications/HTML5/EpochTimestampDateTimeConverter.cs new file mode 100644 index 00000000..a500a885 --- /dev/null +++ b/src/Majorsoft.Blazor.Components.Notifications/HTML5/EpochTimestampDateTimeConverter.cs @@ -0,0 +1,21 @@ +using System; +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace Majorsoft.Blazor.Components.Notifications +{ + internal class EpochTimestampDateTimeConverter : JsonConverter + { + public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + long ms = reader.GetInt64(); + return DateTimeOffset.FromUnixTimeMilliseconds(ms).DateTime; + } + + public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options) + { + long ms = new DateTimeOffset(value).ToUnixTimeMilliseconds(); + writer.WriteNumberValue(ms); + } + } +} diff --git a/src/Majorsoft.Blazor.Components.Notifications/HTML5/HtmlNotificationData.cs b/src/Majorsoft.Blazor.Components.Notifications/HTML5/HtmlNotificationData.cs index 32cee5fd..a582dd63 100644 --- a/src/Majorsoft.Blazor.Components.Notifications/HTML5/HtmlNotificationData.cs +++ b/src/Majorsoft.Blazor.Components.Notifications/HTML5/HtmlNotificationData.cs @@ -1,4 +1,5 @@ using System; +using System.Text.Json.Serialization; namespace Majorsoft.Blazor.Components.Notifications { @@ -71,6 +72,7 @@ public abstract class HtmlNotificationData /// /// Specifies the time at which a notification is created or applicable (past, present, or future). /// + [JsonConverter(typeof(EpochTimestampDateTimeConverter))] public DateTime Timestamp { get; set; } = DateTime.UtcNow; ///