Implementation of UTC DateTime

Topics: EF Runtime, General
Nov 8, 2013 at 8:51 AM
Hi,

It could be interresting to force the storage of DateTime in UTC format in Database, specificly for Azure. So the entity datetime property always use the local datetime.

This will avoid going through this method with all datetime properties : Convert value when mapping
Developer
Nov 8, 2013 at 5:24 PM
DateTime type is problematic because it can be of Unspecified kind which can be interpreted as local or UTC if you decide to convert it (or not interpreted at all if you don't convert it). Moreover the SQL Server types DateTime maps to are not time-zone aware too which again results in materializing values to DateTime instances of Unspecified so that the user can interpret them the way they think is correct. In general I would advise using DateTimeOffset type instead of DateTime since it is aware of time zone concepts so there is no ambiguity about how to interpret the value - for more details see notes in the msdn documentation on DateType type and my thoughts on this. Also, note that changing the current EF behavior would be a huge breaking change which would break many existing apps that currently use DateTime type. Applications that currently convert the values to local/utc time zone and that use the raw value would be both most likely broken.
Nov 13, 2013 at 12:19 AM
It would be useful to be able to specify a default DateTime.Kind for materializing objects...
// e.g. 
someEfConfig.DefaultDateTimeKind = DateTimeKind.Utc;
If this is "DateTimeKind.Unspecified" by default then the backwards compatibility issue is covered (well, I think so as a non-EF dev!)
Developer
Nov 13, 2013 at 1:45 AM
Maybe. I think you could also achieve this by listening to the ObjectContext.ObjectMaterialized event and fixing up the entity accordingly.
Nov 13, 2013 at 3:40 AM
moozzyk wrote:
Maybe. I think you could also achieve this by listening to the ObjectContext.ObjectMaterialized event and fixing up the entity accordingly.
Yes - that's what we are looking at. In short reflecting over properties and applying the Kind as Utc... Just feels a bit wrong as it's just been hydrated!!

:)