3

Environment:

  • ASP.NET Web Forms
  • 3-tier
  • .Net 4.6.1
  • SQL Server 2014

I have code to convert UTC to my local timezone. This the proceeding is in my data layer. Is this the correct layer? The following snippet is when a user gets data. I am not performing any conversion when user saves data.

DateTime? dateModified = row.GetValueOrDefault<DateTime>("DateModified");

if (dateModified != DateTime.MinValue) dateModified = 
        TimeZoneInfo.ConvertTime((DateTime)dateModified, TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time"));
task.DateModified = dateModified;
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Rod
  • 14,529
  • 31
  • 118
  • 230
  • 1
    Note that you cannot accurately convert between timezones when using `DateTime`. So to start with, you have the wrong type and that mistake likely permeates all layers – Aluan Haddad Apr 08 '18 at 04:37
  • What should I do then? – Rod Apr 08 '18 at 04:38
  • 1
    Start by using the correct types in your database (`datetimeoffset`) but that alone is not sufficient. – Aluan Haddad Apr 08 '18 at 04:42
  • 1
    @AluanHaddad _"...you cannot accurately convert between timezones..."_ - I think you are thinking of _day light savings_. Besides OP is only asking about conversion from UTC to localtime and in that `DataTime` is more than sufficient. _"[The only degree of "time zone awareness" it has is it stores the UTC offset for a particular instant in time. It has no other knowledge of time zones, including the time zone the value was created from, so its only real use is with historical data](https://stackoverflow.com/questions/9073149/difference-between-datetime-and-datetimeoffset)"_ –  Apr 08 '18 at 04:57
  • @MickyD isually local date time is server local which, for web applications at any rate, is probably not desirable. – Aluan Haddad Apr 08 '18 at 05:00
  • That's why I said it isn't _sufficient_ just that it was _necessary_. You also need to store a representation of the timezone. – Aluan Haddad Apr 08 '18 at 05:02
  • I don't know much about Web Forms but if you render the page web server side, then be sure to inject the time converted for the user. Otherwise convert it client-side in the browser as per local machine –  Apr 08 '18 at 05:02
  • _"You also need to store a representation of the timezone"_ - nonsense. Windows Win32 APIs have been converting from UTC to local time and back again way before we even had Internet with no need for UTC time offsets and their half-baked attempts at addressing the overly complex world of shared UTC-offsets and daylight savings –  Apr 08 '18 at 05:03
  • 2
    Depends on where/what you need local time *for*. Usually I like to work with the most culture independent and "information rich" data for as long as possible. So for example on a web application, convert input on client side and only ever send UTC data to the server. And also only send UTC data from the server back and let client side handle localization *just* before displaying it. – Corak Apr 08 '18 at 05:04
  • 2
    All stored values for historical data should be in UTC. Convert them for display and input. It does not matter whether this is done in JavaScript in the GUI or in the ASP.Net provided it is stored in the database correctly. Added: For servers you should generally set the server timezone to UTC to a) avoid issues with daylight savings transitions and b) reduce accidental usage of local time and c) remind you at all times that you must always convert times. – Ben Apr 08 '18 at 17:07

0 Answers0