简体   繁体   中英

Daylight saving changes affecting UTC conversion

Basically I'm converting local dates stored in the database into UTC. But I've read somewhere that daylight saving rules have changed in 2007. So does the Date.ToUniversalTime() function still work correctly. Basically the dates before 2007(when the new rules came into effect) would be converted correctly but the dates after that would be not. Am I right here? Or would.Net take care of the conversion internally ie depending upon the different daylight saving rules?

EDIT: Dates are stored in DB as local times. I am converting it into UTC. So a date such as '9 March 2005' should be converted using day light rules of 2005 instead of today's rules. The rules changed in the US in 2007. So the date is coming out wrong by one hour.

It will depend on which version of .NET you're using and possibly which version of Windows you're using. .NET 3.5 has the TimeZoneInfo class which includes historical changes etc - before then, the support was far more patchy, unfortunately.

I would expect ToUniversalTime() to take that into account. Have you tried and checked the result with dates from before and after the DST change?

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. 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.

If the UTC offset isn't known then it is virtually impossible to know how to convert it to 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.

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. 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; at least then you'll have a reference

see this post for more reasons never to store db times in local time

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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