.net - Assume local instead of Unspecified DateTime from SQL Server -


because sql server datatype datetime not keep timezone information, sqlclient in .net instantiating datetime objects kind set datetimekind.unspecified. of cause right thing - theoretically. every application have worked on has stored datetime assuming local, , lived happy long time.

but when globalization demands arise, there trouble assumption / convention. instance json serialization not trusted!

so question: possible either on database, or on web server, enforce datetime read database (via sqlclient.datareader.getdatetime) assumed datetimekind.local?

i don't think there's way enforce datetime pulled reader automatically assumed local time, it's easy enough change afterwards using specifykind method:

var unspecifiedvalue = yourreader.getdatetime(yourcolumn); var localvalue = datetime.specifykind(unspecifiedvalue, datetimekind.local); 

you wrap in extension method if prefer (either hardcoding datetimekind i've done here, passing in argument, or pulling config):

var localvalue = yourreader.getlocaldatetime(yourcolumn);  // ...  public static datetime getlocaldatetime(this idatarecord source, int i) {     if (source == null) throw new argumentnullexception("source");     return datetime.specifykind(source.getdatetime(i), datetimekind.local); } 

Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -