C# 从 DateTime 获取时区

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/576740/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 08:56:59  来源:igfitidea点击:

Get timezone from DateTime

c#.netdatetimetimezone

提问by dbkk

Does the .Net DateTime contain information about time zone where it was created?

.Net DateTime 是否包含有关创建时区的信息?

I have a library parsing DateTime from a format that has "+zz" at the end, and while it parses correctly and adjusts a local time, I need to get what the specific time zone was from the DateTime object.

我有一个库从末尾带有“+zz”的格式解析 DateTime,虽然它正确解析并调整本地时间,但我需要从 DateTime 对象中获取特定时区。

Is this possible at all? All I can see is DateTime.Kind, which specifies if time is local or UTC.

这可能吗?我只能看到 DateTime.Kind,它指定时间是本地时间还是 UTC。

采纳答案by Jon Skeet

DateTime itself contains no real timezone information. It mayknow if it's UTC or local, but not what local really means.

DateTime 本身不包含实际时区信息。它可能知道它是 UTC 还是本地,但不知道本地的真正含义。

DateTimeOffset is somewhat better - that's basically a UTC time and an offset. However, that's still not really enough to determine the timezone, as many different timezones can have the same offset at any one point in time. This sounds like it may be good enough for you though, as all you've got to work with when parsing the date/time is the offset.

DateTimeOffset 稍微好一些 - 这基本上是一个 UTC 时间和一个偏移量。然而,这仍然不足以确定时区,因为许多不同的时区在任何一个时间点都可以具有相同的偏移量。这听起来对您来说可能已经足够了,因为在解析日期/时间时您需要处理的只是偏移量。

The support for time zones as of .NET 3.5 is a lot better than it was, but I'd really like to see a standard "ZonedDateTime" or something like that - a UTC time and an actual time zone. It's easy to build your own, but it would be nice to see it in the standard libraries.

从 .NET 3.5 开始,对时区的支持比以前好得多,但我真的很想看到一个标准的“ZonedDateTime”或类似的东西 - UTC 时间和实际时区。构建你自己的很容易,但在标准库中看到它会很高兴。

EDIT: Nearly four years later, I'd now suggest using Noda Timewhich has a rather richer set of date/time types. I'm biased though, as the main author of Noda Time :)

编辑:将近四年后,我现在建议使用Noda Time,它具有更丰富的日期/时间类型。不过,作为 Noda Time 的主要作者,我有偏见 :)

回答by Gerrie Schenck

No.

不。

A developer is responsible for keeping track of time-zone information associated with a DateTime value via some external mechanism.

开发人员负责通过某种外部机制跟踪与 DateTime 值关联的时区信息。

A quote from an excellent article here. A must read for every .Net developer.

此处引用一篇优秀文章。每个 .Net 开发人员都必须阅读。

So my advice is to write a little wrapper class that suits your needs.

所以我的建议是编写一个适合您需要的小包装类。

回答by tehvan

From the API (http://msdn.microsoft.com/en-us/library/system.datetime_members(VS.71).aspx) it does not seem it can show the name of the time zone used.

从 API ( http://msdn.microsoft.com/en-us/library/system.datetime_members(VS.71).aspx) 看来,它似乎无法显示所用时区的名称。

回答by Cheeso

There is a public domain TimeZone library for .NET. Really useful. It will answer your needs.

.NET有一个公共域 TimeZone 库。真的很有用。它将满足您的需求。

Solving the general-case timezone problem is harder than you think.

解决一般情况下的时区问题比您想象的要困难。

回答by Shekhar

You could use TimeZoneInfoclass

您可以使用TimeZoneInfo

The TimeZone class recognizes local time zone, and can convert times between Coordinated Universal Time (UTC) and local time. A TimeZoneInfo object can represent any time zone, and methods of the TimeZoneInfo class can be used to convert the time in one time zone to the corresponding time in any other time zone. The members of the TimeZoneInfo class support the following operations:

TimeZone 类识别本地时区,并且可以在协调世界时 (UTC) 和本地时间之间转换时间。TimeZoneInfo 对象可以表示任何时区,TimeZoneInfo 类的方法可用于将一个时区的时间转换为任何其他时区的相应时间。TimeZoneInfo 类的成员支持以下操作:

  1. Retrieving a time zone that is already defined by the operating system.

  2. Enumerating the time zones that are available on a system.

  3. Converting times between different time zones.

  4. Creating a new time zone that is not already defined by the operating system.

    Serializing a time zone for later retrieval.

  1. 检索操作系统已定义的时区。

  2. 枚举系统上可用的时区。

  3. 在不同时区之间转换时间。

  4. 创建操作系统尚未定义的新时区。

    序列化时区以供以后检索。

回答by Rob

Generally the practice would be to pass data as a DateTime with a "timezone" of UTC and then pass a TimeZoneInfo object and when you are ready to display the data, you use the TimeZoneInfo object to convert the UTC DateTime.

通常的做法是将数据作为具有 UTC 的“时区”的 DateTime 传递,然后传递一个 TimeZoneInfo 对象,当您准备好显示数据时,您可以使用 TimeZoneInfo 对象来转换 UTC DateTime。

The other option is to set the DateTime with the current timezone, and then make sure the "timezone" is unknown for the DateTime object, then make sure the DateTime is again passed with a TimeZoneInfo that indicates the TimeZone of the DateTime passed.

另一个选项是使用当前时区设置 DateTime,然后确保 DateTime 对象的“时区”未知,然后确保再次使用 TimeZoneInfo 传递 DateTime,该 TimeZoneInfo 指示传递的 DateTime 的时区。

As others have indicated here, it would be nice if Microsoft got on top of this and created one nice object to do it all, but for now you have to deal with two objects.

正如其他人在此处指出的那样,如果 Microsoft 能够在此基础上创建一个不错的对象来完成所有工作,那就太好了,但现在您必须处理两个对象。

回答by henryalligator

DateTime does not know its timezone offset. There is no built-in method to return the offset or the timezone name (e.g. EAT, CEST, EST etc).

DateTime 不知道它的时区偏移量。没有内置方法来返回偏移量或时区名称(例如 EAT、CEST、EST 等)。

Like suggested by others, you can convert your date to UTC:

就像其他人建议的那样,您可以将日期转换为 UTC:

DateTime localtime = new DateTime.Now;
var utctime = localtime.ToUniversalTime();

and then only calculate the difference:

然后只计算差异:

TimeSpan difference = localtime - utctime;

Also you may convert one time to another by using the DateTimeOffset:

您也可以使用 DateTimeOffset 将一次转换为另一次:

DateTimeOffset targetTime = DateTimeOffset.Now.ToOffset(new TimeSpan(5, 30, 0));

But this is sort of lossy compression - the offset alone cannot tell you which time zone it is as two different countries may be in different time zones and have the same time only for part of the year (eg. South Africa and Europe). Also, be aware that summer daylight saving time may be introduced at different dates (EST vs CET - a 3-week difference).

但这是一种有损压缩 - 仅偏移量无法告诉您它是哪个时区,因为两个不同的国家可能处于不同的时区并且仅在一年中的部分时间(例如南非和欧洲)具有相同的时间。此外,请注意,夏季夏令时可能会在不同日期(美国东部时间与欧洲中部时间 - 3 周的差异)引入。

You can get the name of your local system time zone using TimeZoneInfo class:

您可以使用 TimeZoneInfo 类获取本地系统时区的名称:

TimeZoneInfo localZone = TimeZoneInfo.Local;
localZone.IsDaylightSavingTime(localtime) ? localZone.DaylightName : localZone.StandardName

I agree with Gerrie Schenck, please read the article he suggested.

我同意 Gerrie Schenck 的观点,请阅读他建议的文章。