我正在使用Google TimeZone API给我基于地址的UTC偏移量。例如,对于利马市,它返回-18000秒。
我该如何在DateTimeOffset中使用此信息,并使其仅更改它的Offset属性,以秒为单位计入差异,以便显示当前时间,但Offset会像这样改变呢?
DateTime = {12-01-2020 21:15:33} => DateTimeOffset = {12-01-2020 21:15:33 -05:00 }
在巴黎,“偏移”属性应为+01:00。
我正在使用Google TimeZone API给我基于地址的UTC偏移量。例如,对于利马市,它返回-18000秒。
我该如何在DateTimeOffset中使用此信息,并使其仅更改它的Offset属性,以秒为单位计入差异,以便显示当前时间,但Offset会像这样改变呢?
DateTime = {12-01-2020 21:15:33} => DateTimeOffset = {12-01-2020 21:15:33 -05:00 }
在巴黎,“偏移”属性应为+01:00。
我想我已经知道了。这似乎可行:
DateTimeOffset now = DateTimeOffset.UtcNow; //{13-01-2020 07:39:04 +00:00}
TimeSpan timeSpanInterval = TimeSpan.FromSeconds(googleTimeZone.dstOffset + googleTimeZone.rawOffset); // {-05:00:00}
dateTimeOffset = now.ToOffset(timeSpanInterval); // {13-01-2020 02:39:04 -05:00}
也许可以用另一种方法来完成,但这似乎以我想要的方式设置了Offset属性。