Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Daylight saving changes affecting UTC conversion

Tags:

c#

.net

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

like image 675
Malik Daud Ahmad Khokhar Avatar asked Oct 21 '08 13:10

Malik Daud Ahmad Khokhar


3 Answers

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.

like image 190
Jon Skeet Avatar answered Oct 26 '22 19:10

Jon Skeet


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.

like image 1
Isak Savo Avatar answered Oct 26 '22 20:10

Isak Savo


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.

like image 1
Tomalak Avatar answered Oct 26 '22 18:10

Tomalak