c# - 根据机器本地设置,将带有时区的日期时间解析为 DateTimeOffset 的行为会有所不同
问题描述
使用此日期调用 api:
2021-01-09T21:13:00 +00:00
或者
2021-01-09T21:13:00 +01:00
我将此日期转换为根据机器的本地设置以不同方式转换的 api,是否可以隔离此行为并始终在 UTC 时区获取日期?
public class Movimento
{
public int Id { get; set; }
[JsonConverter(typeof(ExperimentalConverter))]
public DateTimeOffset MyDate { get; set; }
.......
}
public class ExperimentalConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(DateTimeOffset) || objectType == typeof(DateTime);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
/*
=============IF MY MACHINE LOCAL TIME IS SET TO UTC + 01:00 ========================
i got "09/01/2021 21:13:00" --> when the client call the api passing --> "2021-01-09T21:13:00+01:00"
i got "09/01/2021 22:13:00" --> when the client call the api passing --> "2021-01-09T21:13:00+00:00"
=============IF MY MACHINE LOCAL TIME IS SET TO UTC (UTC + 00:00) ========================
i got "09/01/2021 20:13:00" --> when the client call the api passing --> "2021-01-09T21:13:00+01:00"
i got "09/01/2021 21:13:00" --> when the client call the api passing --> "2021-01-09T21:13:00+00:00"
*/
var parsedData = (DateTime)reader.Value; // 09/01/2021 21:13:00 when the api pass "2021-01-09T21:13:00+01:00"
//i need a way to uderstad if the received date is in UTC format or UTC+01:00 indipendently from the local settings of the machine, so i can later riapply the offset i need
//"parsedData.Kind" give me always the value Local (because the date in this method is already translates according to the local settings of the machine
.......
}
解决方案
通常,当您解析 a 时DateTimeOffset
,将保留偏移量。但不幸的是,当您将 aJsonConverter
应用于DateTimeOffset
属性时,reader.Value
遗嘱(使用默认设置)包含 aDateTime
偏移量已经受到计算机本地时区的影响。
要解决此问题,DateParseHandling.DateTimeOffset
请在序列化设置中进行设置。
JsonConvert.DefaultSettings = () => new JsonSerializerSettings
{
DateParseHandling = DateParseHandling.DateTimeOffset
};
然后reader.Value
将包含一个DateTimeOffset
,您可以将其拆箱。
此外,您可以使用它JsonConverter<T>
来简化代码,并且不需要处理DateTime
解析(除非您的问题中没有描述其他要求)。
public class UtcDateTimeOffsetConverter : JsonConverter<DateTimeOffset>
{
public override DateTimeOffset ReadJson(JsonReader reader, Type objectType, DateTimeOffset existingValue, bool hasExistingValue, JsonSerializer serializer)
{
// First get the DateTimeOffset exactly as presented (asserting never-null)
var value = (DateTimeOffset) reader.Value!;
// Then convert it to UTC before returning it.
return value.ToUniversalTime();
}
public override void WriteJson(JsonWriter writer, DateTimeOffset value, JsonSerializer serializer)
{
// Convert the value to UTC and write it to JSON using a Z suffix
writer.WriteValue(value.UtcDateTime);
}
}
我还为您提供了一个WriteJson
对这类事情有意义的实现,在输出 JSON 中使用 aZ
而不是。+00:00
推荐阅读
- python - 我正在尝试将多个 csv 文件(13 个文件)合并为一个(按列),而我得到的结果不是按列
- sharepoint - 使用 Graph Api 获取网站集管理员
- rust - 是否可以为 QNX 编译?
- angular - 当我使用 2 个单独的指令时,Angulare 指令只有一个实例有效
- python - 异步sqlalchemy,完成的任务数与数据库中的记录不同
- javascript - 无法在 datetimepicker tempus dominus 中链接日期开始和日期结束
- excel - 需要根据周数和日期打开文件
- angular - How can I patch a nested form Array?
- unix - 比较linux中的两个文件并将结果输出到其中一个文件中
- google-cloud-platform - Google Cloud Airflow Composer 产生磁盘成本“WHY”还是“FROM WHERE”?