简体   繁体   中英

Managing Time zone and Daylight Saving in SQL Server

We currently are trying to implement time zone management in our application. Our server is in India. We will be saving all entries in server time. But when coming to the client side, we need to show the data in the client time. So I came into this conclusion that getting the client time zone from the front end and converting the server time to client time using the code below in the database will solve the problem.

DECLARE @ServerTime DATETIMEOFFSET(7) = '2015-02-21 22:06:08.6862774 +05:30'   -- My server time
DECLARE @ClientTime DATETIMEOFFSET(7) = '2015-02-21 12:38:09.5421899 -04:00' -- My Client Time

SELECT SWITCHOFFSET(@ServerTime, DATEPART(TZ, @ClientTime))

My question is

  • Is there any better option?
  • Can this be done from the front end (C#)?

Last and most important question is:

  • How can I manage if the client machine is in daylight saving?

Any support will be appreciated.

You can do this in a much simpler way in .NET, since .NET has some "automatic" mechanisms to deal with this.

For example: if your client app invokes a web service and sends as argument a DateTime in local time (date.Kind == DateTimeKind.Local) the date is serialized with the offset ("2015-08-19T14:21+01:00") on the server side the date will be deserialized and converted to the server's local time. For instance, using the date above and if the server is in UTC the DateTime object would have the value "2015-08-19 13:21:00" (however you will not have any information about the timezone where the client is and where the date was created). If the date is sent again to the client the inverse conversion is made and the date will have the original value on the client. This way the server will always process dates using local time and the client will always process dates in local time, although they do not know the timezone of each other.

I believe this mechanism does not deal well with ancient past dates because of daylight saving time, this is probably being limited by the information windows has about daylight saving time (as an example for the timezone "E. South America Standard Time" windows 8 only has DST information from 2006 to 2040 meaning that dates previous to 2006 could be misunderstood).

Having said that, I believe the best solution to deal with different timezones is to use UTC and convert to local time when displaying the date to the user, or always use DateTimeOffset instead of DateTime (DateTimeOffset also has information about the date timezone).

While it is better to do this in client code, if you decide you need to work with time zones directly in SQL Server, you can now use my SQL Server Time Zone Support project.

Example:

SELECT Tzdb.UtcToLocal('2015-07-01 00:00:00', 'America/Los_Angeles')

You can use:

SELECT CONVERT(datetime,'2019-03-11 11:59:59') 
AT TIME ZONE 'UTC' 
AT TIME ZONE 'US Eastern Standard Time';

It also handles daylight saving time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM