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

查看:132
本文介绍了从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 ,它具有更丰富的日期/时间类型。我有偏见,作为Noda时间的主要作者:)

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天全站免登陆