Storing a c# DateTimeOffset value in a SQL Server 2005 database

橙三吉。 提交于 2019-12-12 09:39:07

问题


I want to store a a c# DateTimeOffset value in a SQL Server 2005 database.

Sql 2008 has this as a built-in-type, but SQL Server 2005 does not.

The DateTimeOffset structure has a DateTime value wich I store as DateTime, an an Offset property (of type TimeSpan). Since this is the time zone relative to UTC, presumably it is usally at a whole number of hours or half-hours.

Suggestions on how best to store this in a SQL Server 2005 database?


回答1:


It's not a good idea to assume that an offset is a number of hours or half-hours - there are certainly quarter-hour timezones around.

Using milliseconds for the offset is probably the most flexible, but I'd argue that minutes is a lot easier to read. If you're ever going to look at the "raw" data in the database, it's easier to understand value 60 = 1 hour than 3600000. I can't imagine you really needing fractions of minutes as the offset.




回答2:


Normalize all DateTimeOffsets to a common offset, preferably UTC. Then just store the DateTime as usual. Upon extraction restore the offset, which should be a constant. This doesn't retain the originating offset but the offset is ambiguous to a timezone anyway.

If you actually need to know the date/time origin, then you'd need to store some timezone information. This is because a simple offset can't unambiguously represent the origin of a time. Please see (the somewhat confusing) MSDN documentation about Choosing Between DateTime, DateTimeOffset, and TimeZoneInfo.




回答3:


store the datetime as datetime and the offset as milliseconds (bigint)



来源:https://stackoverflow.com/questions/195606/storing-a-c-sharp-datetimeoffset-value-in-a-sql-server-2005-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!