mirror of
https://github.com/Crunchy-DL/Crunchy-Downloader.git
synced 2026-01-11 20:10:26 +00:00
33 lines
No EOL
1.4 KiB
C#
33 lines
No EOL
1.4 KiB
C#
using System;
|
|
using Newtonsoft.Json;
|
|
|
|
namespace CRD.Utils.JsonConv;
|
|
|
|
public class UtcToLocalTimeConverter : JsonConverter<DateTime>{
|
|
public override DateTime ReadJson(JsonReader reader, Type objectType, DateTime existingValue, bool hasExistingValue, JsonSerializer serializer){
|
|
try{
|
|
return reader.Value switch{
|
|
null => DateTime.MinValue,
|
|
DateTime dateTime when dateTime.Kind == DateTimeKind.Utc => dateTime.ToLocalTime(),
|
|
DateTime dateTime => dateTime,
|
|
string dateString => TryParseDateTime(dateString),
|
|
_ => throw new JsonSerializationException($"Unexpected token parsing date. Expected DateTime or string, got {reader.Value?.GetType()}.")
|
|
};
|
|
} catch (Exception ex){
|
|
Console.Error.WriteLine("Error deserializing DateTime", ex);
|
|
}
|
|
return DateTime.UnixEpoch;
|
|
}
|
|
|
|
private DateTime TryParseDateTime(string dateString){
|
|
if (DateTime.TryParse(dateString, out DateTime parsedDate)){
|
|
return parsedDate.Kind == DateTimeKind.Utc ? parsedDate.ToLocalTime() : parsedDate;
|
|
}
|
|
|
|
throw new JsonSerializationException($"Invalid date string: {dateString}");
|
|
}
|
|
|
|
public override void WriteJson(JsonWriter writer, DateTime value, JsonSerializer serializer){
|
|
writer.WriteValue(value);
|
|
}
|
|
} |