如何更改UTC时区并获取实际时间?
How to change UTC timezone and get actual time?
我想要实现的是,通过使用 int 值,更改 UTC 日期时间并从不同时区接收时间。
Int 值应该是:
0 = UTC+00:00
1 = UTC+01:00
...
按照逻辑,它应该是这样的:
int timezoneInt = 1;
var newDate = DateTime.UtcNow.AddMinutes(timezoneInt*60);
但问题是这不包括 summer/winter 时间。
例如:
我的位置是 UTC+02:00,时间是 09:20 上午。我需要获得 UTC+00:00(等于 DateTime.UtcNow
和 应该是 (?) 07:20 AM)。由于夏令时,现在 .UtcNow
是 06:20 上午,所以我不能只将 60 分钟乘以 int 值,我还需要以某种方式包括夏令时因素。
我应该如何做到这一点,或者我缺少或了解谁?
编辑:
被标记为重复。好吧,here 我没有看到任何有助于通过使用 int 值作为时区来改变时间的东西。
夏令时调整需要一个广泛的时区数据库,该数据库还记录该时区更改为夏令时的日期和时间,以及新的 UTC 偏移量。
DST 数据库不容易建立(在某些情况下甚至使用)并且必须手动研究和维护,因为它们是政治性的,而不是技术性的 - 各国可以根据需要更改他们的夏令时日期,因此数据库还需要记录历史日期。
.NET Framework 有一个内置的 TimeZoneInfo
class(废弃了 TimeZone
class),它利用内置的时区数据库来Windows 或者主机 OS 是什么。 Windows' 时区数据库使用全名来标识时区,而 Linux 和 tzdb 使用像 America/New_York
.
这样的 ID
请注意,通常您不应该自己执行这些计算,因为总是有许多边缘情况需要注意。只需使用 DateTimeOffset
.
此外,在 UTC 偏移量和时区之间存在 not 1:1 映射:不同的时区共享相同的 UTC 偏移量但具有不同的夏令时规则(例如英国时区使用 UTC+0 作为其正常的 UTC 偏移量,但在夏季使用 UTC+1,但是如果您看到 "UTC+1" 则可能是夏季的英国时区或西非时区,例如阿尔及利亚的 UTC+1 , 但根本不使用夏令时。
逐字复制自the timezone tag wiki:
Time Zone != Offset
A time zone can not be represented solely by an offset from UTC. Many time zones have more than one offset due to daylight saving time (aka "summer time") rules. The dates that offsets change are also part of the rules for the time zone, as are any historical offset changes. Many software programs, libraries, and web services disregard this important detail, and erroneously call the standard or current offset the "zone". This can lead to confusion, and misuse of the data. Please use the correct terminology whenever possible.
- An offset is just a number that represents how far a particular date/time value is ahead or behind UTC.
- Most offsets are expressed in whole hours.
- But there are many that are 30-minute offset.
- And there are a few that are 45-minute offset.
- A time zone contains much more:
- A name or ID that can be used to identify the zone.
- One or more offsets from UTC
- The specific dates and times that the zone transitions between offsets.
- Sometimes, a separate language-specific display name that can be presented to a user.
One can determine the correct offset, given a time zone and a datetime. But one cannot determine the correct time zone given just an offset.
你问的是如何忽略这个事实并让它正常工作——这是不可能的。您不能 使用整数偏移量并期望它理解夏令时。任何试图解决这个问题的解决方案都将偏向于一套特定的夏令时规则,这些规则因国家/地区而异。
我想要实现的是,通过使用 int 值,更改 UTC 日期时间并从不同时区接收时间。
Int 值应该是:
0 = UTC+00:00
1 = UTC+01:00
...
按照逻辑,它应该是这样的:
int timezoneInt = 1;
var newDate = DateTime.UtcNow.AddMinutes(timezoneInt*60);
但问题是这不包括 summer/winter 时间。
例如:
我的位置是 UTC+02:00,时间是 09:20 上午。我需要获得 UTC+00:00(等于 DateTime.UtcNow
和 应该是 (?) 07:20 AM)。由于夏令时,现在 .UtcNow
是 06:20 上午,所以我不能只将 60 分钟乘以 int 值,我还需要以某种方式包括夏令时因素。
我应该如何做到这一点,或者我缺少或了解谁?
编辑: 被标记为重复。好吧,here 我没有看到任何有助于通过使用 int 值作为时区来改变时间的东西。
夏令时调整需要一个广泛的时区数据库,该数据库还记录该时区更改为夏令时的日期和时间,以及新的 UTC 偏移量。
DST 数据库不容易建立(在某些情况下甚至使用)并且必须手动研究和维护,因为它们是政治性的,而不是技术性的 - 各国可以根据需要更改他们的夏令时日期,因此数据库还需要记录历史日期。
.NET Framework 有一个内置的 TimeZoneInfo
class(废弃了 TimeZone
class),它利用内置的时区数据库来Windows 或者主机 OS 是什么。 Windows' 时区数据库使用全名来标识时区,而 Linux 和 tzdb 使用像 America/New_York
.
请注意,通常您不应该自己执行这些计算,因为总是有许多边缘情况需要注意。只需使用 DateTimeOffset
.
此外,在 UTC 偏移量和时区之间存在 not 1:1 映射:不同的时区共享相同的 UTC 偏移量但具有不同的夏令时规则(例如英国时区使用 UTC+0 作为其正常的 UTC 偏移量,但在夏季使用 UTC+1,但是如果您看到 "UTC+1" 则可能是夏季的英国时区或西非时区,例如阿尔及利亚的 UTC+1 , 但根本不使用夏令时。
逐字复制自the timezone tag wiki:
Time Zone != Offset
A time zone can not be represented solely by an offset from UTC. Many time zones have more than one offset due to daylight saving time (aka "summer time") rules. The dates that offsets change are also part of the rules for the time zone, as are any historical offset changes. Many software programs, libraries, and web services disregard this important detail, and erroneously call the standard or current offset the "zone". This can lead to confusion, and misuse of the data. Please use the correct terminology whenever possible.
- An offset is just a number that represents how far a particular date/time value is ahead or behind UTC.
- Most offsets are expressed in whole hours.
- But there are many that are 30-minute offset.
- And there are a few that are 45-minute offset.
- A time zone contains much more:
- A name or ID that can be used to identify the zone.
- One or more offsets from UTC
- The specific dates and times that the zone transitions between offsets.
- Sometimes, a separate language-specific display name that can be presented to a user.
One can determine the correct offset, given a time zone and a datetime. But one cannot determine the correct time zone given just an offset.
你问的是如何忽略这个事实并让它正常工作——这是不可能的。您不能 使用整数偏移量并期望它理解夏令时。任何试图解决这个问题的解决方案都将偏向于一套特定的夏令时规则,这些规则因国家/地区而异。