19 lines
788 B
C#
19 lines
788 B
C#
using System.Text.Json;
|
||
|
||
namespace IM_API.Tools
|
||
{
|
||
public class UtcDateTimeConverter : System.Text.Json.Serialization.JsonConverter<DateTime>
|
||
{
|
||
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||
=> reader.GetDateTime();
|
||
|
||
public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
|
||
{
|
||
// 如果 Kind 已经是 Utc,直接输出。
|
||
// 如果不是,先 SpecifyKind 再输出,确保不会发生时区偏移计算
|
||
var utcDate = value.Kind == DateTimeKind.Utc ? value : DateTime.SpecifyKind(value, DateTimeKind.Utc);
|
||
writer.WriteStringValue(utcDate.ToString("yyyy-MM-ddTHH:mm:ssZ"));
|
||
}
|
||
}
|
||
}
|