[服务器处理时区上线测试]

This commit is contained in:
2024-02-21 13:58:34 +08:00
parent 1d4f58f362
commit 292bcd7100
9 changed files with 391 additions and 78 deletions
@@ -0,0 +1,60 @@
//using Microsoft.AspNetCore.Mvc.Filters;
//using Microsoft.AspNetCore.Mvc;
//using Newtonsoft.Json;
//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
//using System.Threading.Tasks;
//using System.Text.RegularExpressions;
//namespace IRaCIS.Core.Application.BusinessFilter
//{
// public class ConvertToClientTimeResultFilter : IAsyncResultFilter
// {
// public async Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next)
// {
// //JsonConvert.DeserializeObject<T>(jsonStr, new JsonSerializerSettings
// //{
// // DateTimeZoneHandling = DateTimeZoneHandling.Local
// //});
// if (context.Result is ObjectResult objectResult)
// {
// var statusCode = objectResult.StatusCode ?? context.HttpContext.Response.StatusCode;
// if (statusCode == 200)
// {
// var result = objectResult.Value;
// // 将数据中的 DateTime 值转换为 DateTimeOffset,指定时区为东八区
// ConvertDateTimeToDateTimeOffset(result);
// // 使用 Newtonsoft.Json 序列化转换后的数据
// string json = JsonConvert.SerializeObject(result);
// // 重新设置 JsonResult 的值为处理后的数据
// context.Result = new JsonResult(json);
// }
// }
// // 执行动作方法
// await next();
// }
// private void ConvertDateTimeToDateTimeOffset(object data)
// {
// // 假设客户端时区为东八区
// TimeZoneInfo clientTimeZone = TimeZoneInfo.FindSystemTimeZoneById("China Standard Time");
// // 将对象中的 DateTime 值转换为 DateTimeOffset,指定时区为东八区
// string json = JsonConvert.SerializeObject(data);
// data = json.Replace("\"DateTime\":", "\"DateTimeOffset\":");
// }
// }
//}
@@ -0,0 +1,83 @@
using Newtonsoft.Json.Converters;
using Newtonsoft.Json;
using System;
using SharpCompress.Writers;
namespace IRaCIS.Core.API._ServiceExtensions.NewtonsoftJson
{
public class ExportExcelDateConverter : DateTimeConverterBase
{
private readonly TimeZoneInfo _clientTimeZone;
public ExportExcelDateConverter(TimeZoneInfo clientTimeZone)
{
_clientTimeZone = clientTimeZone;
}
public override bool CanConvert(Type objectType)
{
return objectType == typeof(DateTime) || objectType == typeof(DateTime?);
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
DateTime? nullableDateTime = value as DateTime?;
if (nullableDateTime != null && nullableDateTime.HasValue)
{
// 将服务器时间转换为客户端时间
DateTime clientTime = TimeZoneInfo.ConvertTime(nullableDateTime.Value, TimeZoneInfo.Local, _clientTimeZone);
writer.WriteValue(clientTime);
}
else
{
writer.WriteNull();
}
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
DateTime? nullableDateTime = reader.Value as DateTime?;
if (nullableDateTime != null && nullableDateTime.HasValue)
{ // 服务器时区的时间转为客户端时间
var clientZoneTime = TimeZoneInfo.ConvertTime(nullableDateTime.Value, TimeZoneInfo.Local, _clientTimeZone);
return clientZoneTime;
}
return reader.Value;
}
}
public class ExportExcelConverterDate
{
public static T ConvertToClientTimeInObject<T>(T obj, string timeZoneId)
{
var clientTimeZone = TimeZoneInfo.FindSystemTimeZoneById(timeZoneId);
if (obj == null)
return obj;
// 将对象序列化为 JSON 字符串
string json = JsonConvert.SerializeObject(obj);
// 将 JSON 字符串反序列化回对象
var deserializedObj = JsonConvert.DeserializeObject<T>(json, new JsonSerializerSettings
{
Converters = { new ExportExcelDateConverter(clientTimeZone) }
});
return deserializedObj!;
}
}
}