Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store TimeZoneInfo objects in a database?

Somewhat misleading title, I know. Never actually wanted to store TimeZoneInfo object themselves: rather, I want to store some culture-neutral identifier, which can then be later used to reconstruct an instance of TimeZoneInfo.

Currently, I'm storing the value of TimeZoneInfo.Id property and it seems to be OK both on English and Russian versions of Windows, but I just wanted to make sure I do the right thing.

like image 851
Anton Gogolev Avatar asked Aug 10 '10 07:08

Anton Gogolev


People also ask

How do databases store different time zones?

Rule 2: Use an Appropriate Data Type Oracle: Use the TIMESTAMP WITH TIME ZONE data type as this can store the timezone of UTC. SQL Server: Use the DATETIMEOFFSET data type, which can store the timezone of UTC. MySQL: Use the TIMESTAMP data type which can include a timezone component.

How do you store datetime?

MySQL comes with the following data types for storing a date or a date/time value in the database: DATE - format YYYY-MM-DD. DATETIME - format: YYYY-MM-DD HH:MI:SS. TIMESTAMP - format: YYYY-MM-DD HH:MI:SS.

Does SQL Server store timezone?

How to Convert UTC to Local Time Zone in SQL Server in SQL Server. SQL Server does not store time zone data when storing timestamps. It uses the host server time as the basis for generating the output of getdate() .


1 Answers

Yes, Id is a non-localized identifier, so that's an appropriate thing to store.

You should be aware of one possible problem though: identifiers can change over time. I don't know whether it's an issue in Windows time zone identifiers, but it certainly occurs in the Olson (zoneinfo) database. For example, I was recently looking at an issue caused by "Pacific/Ponape" changing to "Pacific/Pohnpei".

I suspect that as Microsoft has tighter control over the IDs, they're more likely to remain the same - but even so, countries can change their names, split into different countries (potentially creating new time zones) etc.

I'm not suggesting any fixes for this problem - just highlighting it as a potential issue. Storing the ID is probably the best approach available, but be aware of the potential risks...

like image 189
Jon Skeet Avatar answered Oct 12 '22 13:10

Jon Skeet