如何区分伦敦时间和存储在 DateTimeOffset 中的 UTC?

How do I distinguish London time from UTC stored in DatTimeOffset?

我使用 DateTimeOffset 类型来存储日期和时间。在某些情况下,此类型包含本地时间和与 UTC 的偏移量,在其他情况下 - UTC 时间。

以前我是用DateTime.Kind来区分这些情况的。但是 DateTimeOffset 类型中不存在 Kind 属性。

如何区分 DateTimeOffset 中存储的是零偏移量的伦敦时间还是 UTC 时间?

DateTimeOffset 仅存储 UTC 偏移数据,不存储还包括 DST 属性的时区数据。所以如果你想存储 "what time zone this DateTimeOffset object belongs to" 种数据,你会想要使用 System.TimeZoneInfo。

我的建议是创建一个 class(类似于 "DateTimeOffsetWithTimeZone",它有两个成员,一个 DateTimeOffset 和一个 TimeZoneInfo。在为伦敦时间设置一个实例时,您可以分配给TimeZoneInfo 成员如下:

myInstance.TZInfo = TimeZoneInfo.FindSystemTimeZoneById("GMT Standard Time");