c# - DateTimeOffset display after daylight savings change -


i have question datetimeoffset , daylight savings time. explain question lets assume right date , time is:

11/6/2010  10:15:00  

if run code:

datetimeoffset mytime = datetimeoffset.now; console.writeline("local time: " + mytime.tolocaltime().datetime); 

then result:

local time: 11/6/2010 10:15:00 am

meaning event happened @ 10:15 in morning (my time zone mountain daylight time (-6 offset)).

so, save datetimeoffset sql server 2008 db (as datetimeoffset). next day want display user. daylight savings has expired.

if run above writeline saved off value (from previous day) display?

the offset stored in database -6. daylight savings over, current offset -7. understand documentation, first convert time utc time (so takes 10:15 , adds 6 hours (4:15 pm). subtract current offset of local time (4:15 pm - 7 = 9:15 am).

so if math right, when display event, show occurred @ 9:15 rather 10:15 am.

this not good. want store time zone information, need times stay static in same time zone. (meaning if event happened @ 10:15 in utah, next time @ (in utah), need see @ 10:15 am, regardless if daylight savings time change has happened.

i can't think first 1 have issue. others fix this? (or have facts wrong?)

no, won't add current offset - it'll add the offset @ date still -6. should still display 10:15am, because knows date involved, , time zone rules in force on date.

you may want store simple utc time , time zone identifier separately, way. if you're storing time zone, using datetimeoffset won't particularly helpful on utc date/time. (on other hand, it's clearer does represent instant in time - datetime horribly confused type doesn't let express you're trying represent.)

of course i'd encourage @ noda time in biased opinion rather clearer date/time api built-in one... isn't quite ready production use. (we're getting there though...)


Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -