使用此日期调用 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
.......
}