English 中文(简体)
Converting asp.net/sql web app to be ime zone savy
原标题:

Our current app stores all dates via the server s datetime. I m thinking we need to update all datetime values in the database over to UTC time.

Now for displaying these dates back to the user, I know .Net 3.5 has datatypes that are specific to date time offsets. But does anyone see anything wrong with setting an application variable to represent the desired time zone for the site, and then do a dateadd with that offset to display times back to the user? For instance the Eastern Time zone would have a value of "-5".

问题回答

Sounds like to you want to re-invent the wheel. If the application is a web app or geographically distributed, you need to know the time zone of the users. Knowing the server s correct time may not be helpful to them.

See this question for a code example:

How to render local time given UTC datetime values in ASP.Net without using Javascript?

Have a look at Coding Best Practices using DateTime in the .NET Framework. It isn t a tough read and you ll thank yourself for implementing them this way down the road - for example, you won t have to deal with daylight savings changes if you use .NET s provided timezone handling, for example.

Well, as long as you handle daylight savings "properly". Other than that, if your DB has anything to do with web, records should definitively store UTC. UTC does not "jump" on daylight savings time, so there is no trouble with sorting records entered during time change and datediff calculus is always ok.

In the latest version of .NET you get the TimeZoneInfo class that helps with converting dates.

You store the dates in GMT, also store the user s time zone, and then convert the dates each time you need to display them. The hard part is dealing with daylight savings time. The TimeZoneInfo class should help with this. Before that class was available you had to do it all yourself, storing tables of data on time zones and the date ranges of daylight savings time in each time zone.





相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签