Skip to content

Commit

Permalink
try to use custom UnixDateTimeConverter
Browse files Browse the repository at this point in the history
  • Loading branch information
granstel committed Jun 10, 2024
1 parent e208167 commit bce7ba1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Dodo1000Bot.Messengers.Telegram/TelegramStartup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ public void Configure(IWebHostBuilder builder)
services.AddTransient<ITelegramService, TelegramService>();
services.AddTransient<ITelegramBotClient>(RegisterTelegramClient);
services.AddTransient<INotifyService, TelegramNotifyService>();

services.Configure<Microsoft.AspNetCore.Mvc.JsonOptions>(options =>
{
options.JsonSerializerOptions.Converters.Add(new UnixDateTimeConverter());
});
});
}

Expand Down
23 changes: 23 additions & 0 deletions Dodo1000Bot.Messengers.Telegram/UnixDateTimeConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Text.Json;

namespace Dodo1000Bot.Messengers.Telegram;

public class UnixDateTimeConverter : System.Text.Json.Serialization.JsonConverter<DateTime>
{
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.Number)
{
var number = reader.GetInt64();
var offset = DateTimeOffset.FromUnixTimeSeconds(number);
return offset.DateTime;
}

return new DateTime();
}

public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
{
}
}

0 comments on commit bce7ba1

Please sign in to comment.