确定夏令时是否处于活动状态? SQL服务器 [英] Determine if daylight saving time is active? SQL Server
本文介绍了确定夏令时是否处于活动状态? SQL服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个表,其中包含用户,UTC时间偏移量以及他们是否遵守夏时制。有内置的方法来获取正确的用户时间吗?
I have a table with users, UTC time offset, and if they observe daylight saving time. Is there a built in way to get the correct user time?
推荐答案
这并非在所有情况下都有效,但是在美国,将DST定义为3月2日和11月1日之间的快速修复。
This won't work in all cases, but is a quick fix for those in the US w/ DST defined as between the 2nd Sunday of March and the 1st Sunday of November.
DECLARE @Date datetime2 = '2018-11-04 02:01:00'
SELECT
CASE
WHEN @Date between
DATETIMEFROMPARTS(YEAR(@Date), 3, (8 - DATEPART(WEEKDAY, DATEFROMPARTS(YEAR(@Date), 3, 1))) + 8, 02, 00, 00, 00)
and
DATETIMEFROMPARTS(YEAR(@Date), 11, (8 - DATEPART(WEEKDAY, DATEFROMPARTS(YEAR(@Date), 11, 1))) + 1, 02, 00, 00, 00)
THEN 1
ELSE 0
END --Is it dst?
这篇关于确定夏令时是否处于活动状态? SQL服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文