开发者

How do I handle time-stamped data that comes from different time zones?

I am using a couple variables that store DateTime.Now for both database and web services purposes. Someone asked how the code will behave if the applicat开发者_Python百科ion gets deployed to machines in different time zones. The application will manipulate data from different time zones. How do I insure that no problems arise as a result of this?


When you are dealing with different time zones, it is often helpful to convert everything to universal time. It just so happens there is a property of DateTime to give you exactly that.


Let's say a piece of data comes in from a different time zone. Your system gives it a time stamp that is the current time on your machine, and stores it.

Now a user at the location in the other time zone needs to look up when that transaction occurred, and compare it to something in a log file on their system. The time values won't sync up, because their log is using their time and yours is using your time.

The typical solution is to store the times as universal time (also called Greenwich time). When you get a request from another time zone, you either convert the date back to that time zone's time (so they can compare with their logs) or you let them know that you're returning universal time, and they will have to convert themselves (or else switch to using universal time themselves if they aren't already).

Switching to universal time will also deal with the issue of your application being deployed elsewhere, but the deeper issue of time zone comes up way before that.


To add to NickLarsen's correct answer...

GETUTCDATE - SQL Server 2008 UTC date function.

UtcNow() - .Net UTC datetime function.

It is usually a good idea to use UTC on servers and convert to the user's timezone only for display ( or at least as late as possible ).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜