如何确定 .NET 2.0 中给定日期是否为给定时区的夏令时?
我在 .NET 2.0 上,在中等信任下运行(因此 TimeZoneInfo< /a> 和注册表都是不允许的选项)。 我要求用户提供两个日期和一个时区,并且非常希望能够自动确定是否需要调整 DST 的时区。
这可能甚至不是一个有效的场景,除非我有一些非常强大的支持,例如TimeZoneInfo,首先区分所有不同类型的时区。
I'm on .NET 2.0, running under Medium Trust (so TimeZoneInfo and the Registry are not allowed options). I'm asking the user for two dates and a time zone, and would really love to be able to automatically determine whether I need to adjust the time zone for DST.
This probably isn't even a valid scenario unless I have some very robust support, a la TimeZoneInfo, to differentiate between all of the different varieties of Time Zones in the first place.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这里有优秀的时区库:TZ4Net
Excellent timezone library here: TZ4Net
好吧,由于
TimeZoneInfo
被排除在外,您可能无法在框架本身中找到解决方案(但不要引用我的话)。在这种情况下,您是否考虑过反射
TimeZoneInfo
类并使用您在其中找到的内容?Well, since
TimeZoneInfo
is excluded, you're probably not going to find a solution in the framework itself (but don't quote me on that).In which case, have you considered reflectoring the
TimeZoneInfo
class and using what you find there?@多梅尼克,我考虑过,但我更愿意保持合法性,而且我相当确定信息无论如何都必须嵌入到框架中,或者以某种偷偷摸摸的方式从注册表中获取这不需要权限...
@Domenic, I've considered, but I'd prefer to stay legal, and I'm fairly sure the information would have to be embedded into the framework anyway, or grabbed from the registry in some sneaky way that doesn't require permissions...
TZ 数据库 是一个维护良好的时区规则的公共域数据库。 他们提供的数据还有一种编译格式,并且有很多库可用于读取编译数据,例如:ZoneInfo(tz 数据库/Olson 数据库).NET API
The TZ Database is a public domain database of timezone rules that is very well maintained. There is also a compiled format for the data they provide, and there are lots of libraries available to read the compiled data, like this one: ZoneInfo (tz database / Olson database) .NET API
在 .NET 2.0 中,您必须自己编写代码。 它涉及研究各个地区的夏令时法律并将其构建到您自己的数据结构中。 如果您只关心时区的子集(例如仅在美国),那么问题会有所简化,但如果您需要所有全球时区,则需要做很多工作,然后必须每隔一段时间更新代码法律发生变化的几年。 即使最新版本的 .NET 中的新时区对象也需要 Windows 更新才能在法律更改时保持其正确性。
请查看此处,此处,以及 此处了解更多信息。
In .NET 2.0 you have to code this yourself. It involves researching daylight savings time laws in various regions and building that into your own data structures. The problem is somewhat simplified if you only care about a subset of time zones, for example just in the USA, but if you need all global time zones, you have a lot of work to do, and then the code has to be updated every few years when the laws change. Even the new time zone objects in the latest version of .NET will require windows updates to keep them correct as laws change.
Look here, here, and here for more info.