(T-SQL) 最后一个小时有夏令时吗?

(T-SQL) Did daylight savings occur in the last hour?

我有一个进程将使用 table 分区(每个分区为 1 小时),我需要在归档数据时处理夏令时翻转。

例如,上周末它从 1:59:59 变为 1:00:00,因此第二次 1:05am 处的分区代码 运行 不会发生任何事情 -午夜时间已经被关掉了。

然而,当 spring 滚动时,时间从 1:59am 变为 3:00am,因此当作业在 3:05 运行时,它将在凌晨 2 点关闭。 ...保留原始 table.

凌晨 1 点的数据

理论上我可以只查找最旧的非当前分区和数据并翻转那个(分区键是默认的 getdate() 约束),但我想知道是否有某种方法可以使用 AT TIME ZONE确定夏令时有 "occurred",这样我们就可以使用不同的代码来处理仍然存在的较早时间。

谢谢。

也许是这样的。基本上采用您当前的 getdate() 并将 AT TIME ZONE 与观察到的任何时区一起使用。然后使用 DATEPART tz 并比较您当前和之前。

无论服务器时区是什么,使用 AT TIME ZONE 都会为您提供该时区中特定日期时间值的偏移量。

为了比较之前的情况,我认为您需要使用 2 小时,1 小时用于切换,1 小时用于您要检查多长时间。

看看这个:

DECLARE @BeforeDate DATETIME = '2018-11-04 1:59' --Before the change
DECLARE @AfterDate DATETIME = '2018-11-04 3:00' --After the change

--use can use AT TIME ZONE with DATEPART tz which tells you offset in minutes
--I'm in central, any should work for any that observe the time change
--Your offset is different because of the change.

SELECT DATEPART(tz, @BeforeDate AT TIME ZONE 'Central Standard Time')
SELECT DATEPART(tz, @AfterDate AT TIME ZONE 'Central Standard Time')

--using the above you could possibly compare current offset to 2 hours prior to see if they changed.  2 hours, 1 for the switch and 1 for how far back you want to compare.
DECLARE @CurrentDate DATETIME = '2018-11-04 3:00'  --"simulate" getdate() as of the time change

DECLARE @PriorOffSet INT = (SELECT DATEPART(tz, DATEADD(HOUR, -2, @CurrentDate) AT TIME ZONE 'Central Standard Time')) --You'd have to subtract 2 hours to account for the hour shift and the hour back you want to check.
DECLARE @CurrentOffset INT = (SELECT DATEPART(tz, @CurrentDate AT TIME ZONE 'Central Standard Time'))

SELECT @PriorOffSet, @CurrentOffset

IF @PriorOffSet <> @CurrentOffset
    SELECT 'Time changed in the last hour'
ELSE
    SELECT 'No time change in the last hour'