[时区修改-调整名称]

IRC_NewDev
hang 2024-02-21 14:33:38 +08:00
parent 61530be208
commit a5899023b9
3 changed files with 11 additions and 17 deletions

View File

@ -7,6 +7,9 @@ using System.IO;
using System.Text;
using System.Threading.Tasks;
/// <summary>
/// 废弃,没用
/// </summary>
public class TimeZoneAdjustmentMiddleware
{
private readonly RequestDelegate _next;

View File

@ -9,16 +9,14 @@ using System.Globalization;
namespace IRaCIS.Core.API
{
/// <summary>
/// 废弃,没用,不用这种处理方式
/// 序列化,反序列化的时候,处理时间 时区转换
/// </summary>
public class JSONCustomDateConverter : DateTimeConverterBase
public class JSONTimeZoneConverter : DateTimeConverterBase
{
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly string timeZoneId;
private readonly TimeZoneInfo _clientTimeZone;
public JSONCustomDateConverter(IHttpContextAccessor httpContextAccessor)
public JSONTimeZoneConverter(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
@ -37,8 +35,6 @@ namespace IRaCIS.Core.API
}
private static readonly TimeZoneInfo ChinaTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Etc/UTC");
public override bool CanConvert(Type objectType)
{
// 仅支持 DateTime 类型的转换
@ -64,19 +60,14 @@ namespace IRaCIS.Core.API
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
{
DateTime? nullableDateTime = value as DateTime?;
if (nullableDateTime != null && nullableDateTime.HasValue)
{
//第一个参数默认使用系统本地时区 也就是应用服务器的时区
DateTime chinaTime = TimeZoneInfo.ConvertTime(nullableDateTime.Value, _clientTimeZone);
writer.WriteValue(chinaTime);
DateTime clientZoneTime = TimeZoneInfo.ConvertTime(nullableDateTime.Value, _clientTimeZone);
writer.WriteValue(clientZoneTime);
}
else
{

View File

@ -11,7 +11,7 @@ namespace IRaCIS.Core.API
public static void AddNewtonsoftJsonSetup(this IMvcBuilder builder, IServiceCollection services)
{
services.AddHttpContextAccessor();
services.AddScoped<JSONCustomDateConverter>();
services.AddScoped<JSONTimeZoneConverter>();
builder.AddNewtonsoftJson(options =>
{
@ -28,7 +28,7 @@ namespace IRaCIS.Core.API
options.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.RoundtripKind;
//options.SerializerSettings.Converters.Add(new JSONCustomDateConverter()) ;
options.SerializerSettings.Converters.Add(services.BuildServiceProvider().GetService<JSONCustomDateConverter>());
options.SerializerSettings.Converters.Add(services.BuildServiceProvider().GetService<JSONTimeZoneConverter>());
//IsoDateTimeConverter
//options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;