c# - Converting UTC to local time returns strange result -


i have solution of 3 projects:

  1. core
  2. outlook add-in
  3. asp.net website

both, outlook add-in , website use same methods core project data sql server. when write data database, convert datetime values of 2 tables utc time:

poll_start              poll_end 2013-07-31 12:00:00.000 2013-08-01 12:00:00.000 

and

pick_date 2013-07-31 12:00:48.000 2013-07-31 13:00:12.000 

when data in outlook add-in, correct result:

enter image description here enter image description here

when opening same in website, picks fine:

enter image description here

but start , end time "broken" - offset added, bute wrong hours used:

enter image description here

here's code converting, both, outlook , website, use:

private static void converttolocaltime(poll item) {     item.poll_start = item.poll_start.fromutc();     item.poll_end = item.poll_end.fromutc(); }  private static void converttolocaltime(pick pick) {     if (pick.pick_date != null) pick.pick_date = ((datetime)pick.pick_date).fromutc(); } 

and implementation of datetime.fromutc():

public static datetime fromutc(this datetime value) {     var local = timezoneinfo.local;     return timezoneinfo.converttime(value, timezoneinfo.utc, local); } 

i had same result datetime.tolocaltime(). idea?

edit 1:

this how start , end gets displayed on website (end end instead of start):

var startcell = new tablecell         {             text = string.format(                 @"<a href='{0}' title='{2}' target='_blank'>{1:dd.mm.yyyy hh:mm \u\t\czzz}</a>",                     common.gettimeanddatehyperlink(_poll.start, "vote start"),                     _poll.start,                     converttolocaltimezone),             cssclass = "infocontent"         }; 

and picks:

answercell = new tablecell             {                 text = string.format(                     @"<a href='{0}' title='{2}' target='_blank'>{1}</a>",                         common.gettimeanddatehyperlink(ao.time, ao.realanswer),                         ao.realanswer,                         converttolocaltimezone)             }; 

ao.realanswer returns formated datetime string:

return string.format(wholetime == true ? "{0:d}" : @"{0:dd.mm.yyyy hh:mm \u\t\czzz}", time); 

i solved issue now. datetime values start , end didn't correctly converted: values weren't casted local time.

the reason, why website displayed time local time is, sql server stores every datetime value datetimekind.unspecified instead of keeping specified data (e.g. datetimekind.utc) during insert. when reading data server, kinds datetimekind.unspecified, .tostring() of datetime uses local kind. results utc time + local utc offset.


Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

VBA function to include CDATA -