从 DateTime 获取时区 [英] Get timezone from DateTime

查看:73
本文介绍了从 DateTime 获取时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我有一个库可以从末尾带有+zz"的格式解析 DateTime,虽然它可以正确解析并调整本地时间,但我需要从 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.

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

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

推荐答案

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

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

DateTimeOffset 稍微好一些 - 这基本上是 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.

.NET 3.5 对时区的支持比以前好很多,但我真的很想看到标准的ZonedDateTime"或类似的东西——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.

将近四年后,我现在建议使用 Noda Time,它的日期集相当丰富/时间类型.不过,作为《野田时间》的主要作者,我有偏见:)

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

这篇关于从 DateTime 获取时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆