diff --git a/IRaCIS.Core.API/_ServiceExtensions/NewtonsoftJson/JSONTimeZoneConverter.cs b/IRaCIS.Core.API/_ServiceExtensions/NewtonsoftJson/JSONTimeZoneConverter.cs index 37853b02a..6f2623694 100644 --- a/IRaCIS.Core.API/_ServiceExtensions/NewtonsoftJson/JSONTimeZoneConverter.cs +++ b/IRaCIS.Core.API/_ServiceExtensions/NewtonsoftJson/JSONTimeZoneConverter.cs @@ -71,32 +71,25 @@ namespace IRaCIS.Core.API DateTime dateTime; - if (reader.ValueType == typeof(DateTime) || reader.ValueType == typeof(DateTime?)) + // 2. 检查目标类型是否可空 + bool isNullable = objectType == typeof(DateTime?); + + var canConvert = DateTime.TryParse(reader.Value?.ToString(), out dateTime); + + + if (isNullable == false && canConvert == false) { - DateTime? nullableDateTime = reader.Value as DateTime?; - - if (nullableDateTime != null && nullableDateTime.HasValue) - { - dateTime = nullableDateTime.Value; - } - else - { - return null; - } + throw new JsonSerializationException($"Could not convert string to DateTime: {reader.Value} Path {reader.Path}"); } else { - if (DateTime.TryParse((string)reader.Value, out dateTime) == false) + if (canConvert == false) { - - //throw new JsonSerializationException($"Could not convert string to DateTime: {reader.Value} Path {reader.Path}"); return null; } } - - // 将客户端时间转换为服务器时区的时间 var serverZoneTime = TimeZoneInfo.ConvertTime(dateTime, _clientTimeZone, TimeZoneInfo.Local);