简体   繁体   English

在SQL Server中管理时区和夏时制

[英]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#)? 可以从前端(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. 您可以在.NET中以更简单的方式执行此操作,因为.NET具有一些“自动”机制来处理此问题。

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. 例如:如果您的客户端应用程序调用Web服务并以本地时间(date.Kind == DateTimeKind.Local)发送DateTime作为参数,则日期将以偏移量序列化(“ 2015-08-19T14:21 + 01:00 “)在服务器端,日期将被反序列化并转换为服务器的本地时间。 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). 例如,使用上述日期,并且如果服务器使用UTC,则DateTime对象的值将为“ 2015-08-19 13:21:00”(但是,您将不会获得有关客户端所在时区以及所在时区的任何信息。日期已创建)。 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). 我相信由于夏令时,该机制无法很好地处理古代过去的日期,这可能受到信息窗口中有关夏令时的限制(例如,时区“ E.南美标准时间”窗口8仅作为示例)具有2006年至2040年的DST信息,这意味着2006年之前的日期可能会被误解)。

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). 话虽如此,我认为应对不同时区的最佳解决方案是在向用户显示日期时使用UTC并将其转换为本地时间,或者始终使用DateTimeOffset而不是DateTime(DateTimeOffset也包含有关日期时区的信息)。

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. 尽管最好在客户端代码中执行此操作,但是如果您决定需要直接在SQL Server中使用时区,则现在可以使用我的SQL Server时区支持项目。

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. 它还可以处理夏时制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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