简体   繁体   English

影响 UTC 转换的夏令时更改

[英]Daylight saving changes affecting UTC conversion

Basically I'm converting local dates stored in the database into UTC.基本上我将存储在数据库中的本地日期转换为 UTC。 But I've read somewhere that daylight saving rules have changed in 2007. So does the Date.ToUniversalTime() function still work correctly.但是我在某处读到夏令时规则在 2007 年发生了变化。 Date.ToUniversalTime() 函数仍然可以正常工作。 Basically the dates before 2007(when the new rules came into effect) would be converted correctly but the dates after that would be not.基本上 2007 年之前的日期(新规则生效时)将被正确转换,但之后的日期则不会。 Am I right here?我在这里吗? Or would.Net take care of the conversion internally ie depending upon the different daylight saving rules?或者.Net 会根据不同的夏令时规则在内部进行转换吗?

EDIT: Dates are stored in DB as local times.编辑:日期作为当地时间存储在数据库中。 I am converting it into UTC.我正在将其转换为 UTC。 So a date such as '9 March 2005' should be converted using day light rules of 2005 instead of today's rules.因此,诸如“2005 年 3 月 9 日”之类的日期应该使用 2005 年的日光规则而不是今天的规则进行转换。 The rules changed in the US in 2007. So the date is coming out wrong by one hour.美国的规则在 2007 年发生了变化。所以日期出现了一个小时的错误。

It will depend on which version of .NET you're using and possibly which version of Windows you're using. 这取决于您使用的.NET版本以及您正在使用的Windows版本。 .NET 3.5 has the TimeZoneInfo class which includes historical changes etc - before then, the support was far more patchy, unfortunately. .NET 3.5具有TimeZoneInfo类,其中包括历史更改等 - 在此之前,不幸的是,支持更加不完整。

I would expect ToUniversalTime() to take that into account. 我希望ToUniversalTime()能够考虑到这一点。 Have you tried and checked the result with dates from before and after the DST change? 您是否尝试过使用DST更改之前和之后的日期检查结果?

EDIT 编辑

If you know the timezone offset of all the dates in your DB, I definitively recommend you to convert them to UTC at the table level. 如果您知道数据库中所有日期的时区偏移量,我明确建议您在表级别将它们转换为UTC。 You get rid of a whole lot of headaches that way. 你摆脱了很多令人头疼的问题。 Converting to local time for display purposes is easier. 转换为本地时间以便显示更容易。

It depends on how the information is stored in the database. 这取决于信息在数据库中的存储方式。

Hopefully, the data in the db contains the UTC offset, and if so, any changes to daylight saving rules will be irrelevant. 希望数据库中的数据包含UTC偏移量,如果是这样,对夏令时规则的任何更改都将无关紧要。

If the UTC offset isn't known then it is virtually impossible to know how to convert it to UTC. 如果UTC偏移量未知,则几乎不可能知道如何将其转换为UTC。 For example, if the time is stored as an integer with no metadata then the system would have to know when it was added to the db to be able to figure out the corresponding UTC timestamp. 例如,如果时间存储为没有元数据的整数,则系统必须知道何时将其添加到数据库以便能够找出相应的UTC时间戳。

i hate to say it, but you are screwed . 我不想这么说,但是你被搞砸了 Bite the bullet and change the dates in the database to UTC before the problem gets any worse. 在问题变得更糟之前,咬紧牙关并将数据库中的日期更改为UTC。 Your code will become a nightmare of special-case date-math in no time flat if you continue to try to store local times in the database. 如果您继续尝试在数据库中存储本地时间,您的代码将成为特殊情况日期数学的噩梦。

compromise: store both local time and UTC time in separate columns; 妥协:将本地时间和UTC时间存储在不同的列中; at least then you'll have a reference 至少那你会得到一个参考

see this post for more reasons never to store db times in local time 看到这篇文章有更多的理由永远不会在当地时间存储db时间

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM