Для тех, кто застрял в SQL Server 2005 и не хочет или не может использовать udf - и особенно за пределами США - я применил подход @ Bobman и обобщил его.Следующее будет работать в США, Европе, Новой Зеландии и Австралии, с оговоркой, что не все австралийские штаты соблюдают летнее время, даже штаты, которые находятся в одной и той же «базовой» временной зоне.Также легко добавить DST-правила, которые еще не поддерживаются, просто добавьте строку к значениям @calculation
.
-- =============================================
-- Author: Herman Scheele
-- Create date: 20-08-2016
-- Description: Convert UTC datetime to local datetime
-- based on server time-distance from utc.
-- =============================================
create function dbo.UTCToLocalDatetime(@UTCDatetime datetime)
returns datetime as begin
declare @LocalDatetime datetime, @DSTstart datetime, @DSTend datetime
declare @calculation table (
frm smallint,
til smallint,
since smallint,
firstPossibleStart datetime,-- Put both of these with local non-DST time!
firstPossibleEnd datetime -- (In Europe we turn back the clock from 3 AM to 2 AM, which means it happens 2 AM non-DST time)
)
insert into @calculation
values
(-9, -2, 1967, '1900-04-24 02:00', '1900-10-25 01:00'), -- USA first DST implementation
(-9, -2, 1987, '1900-04-01 02:00', '1900-10-25 01:00'), -- USA first DST extension
(-9, -2, 2007, '1900-03-08 02:00', '1900-11-01 01:00'), -- USA second DST extension
(-1, 3, 1900, '1900-03-25 02:00', '1900-10-25 02:00'), -- Europe
(9.5,11, 1971, '1900-10-01 02:00', '1900-04-01 02:00'), -- Australia (not all Aus states in this time-zone have DST though)
(12, 13, 1974, '1900-09-24 02:00', '1900-04-01 02:00') -- New Zealand
select top 1 -- Determine if it is DST /right here, right now/ (regardless of input datetime)
@DSTstart = dateadd(year, datepart(year, getdate())-1900, firstPossibleStart), -- Grab first possible Start and End of DST period
@DSTend = dateadd(year, datepart(year, getdate())-1900, firstPossibleEnd),
@DSTstart = dateadd(day, 6 - (datepart(dw, @DSTstart) + @@datefirst - 2) % 7, @DSTstart),-- Shift Start and End of DST to first sunday
@DSTend = dateadd(day, 6 - (datepart(dw, @DSTend) + @@datefirst - 2) % 7, @DSTend),
@LocalDatetime = dateadd(hour, datediff(hour, getutcdate(), getdate()), @UTCDatetime), -- Add hours to current input datetime (including possible DST hour)
@LocalDatetime = case
when frm < til and getdate() >= @DSTstart and getdate() < @DSTend -- If it is currently DST then we just erroneously added an hour above,
or frm > til and (getdate() >= @DSTstart or getdate() < @DSTend) -- substract 1 hour to get input datetime in current non-DST timezone,
then dateadd(hour, -1, @LocalDatetime) -- regardless of whether it is DST on the date of the input datetime
else @LocalDatetime
end
from @calculation
where datediff(minute, getutcdate(), getdate()) between frm * 60 and til * 60
and datepart(year, getdate()) >= since
order by since desc
select top 1 -- Determine if it was/will be DST on the date of the input datetime in a similar fashion
@DSTstart = dateadd(year, datepart(year, @LocalDatetime)-1900, firstPossibleStart),
@DSTend = dateadd(year, datepart(year, @LocalDatetime)-1900, firstPossibleEnd),
@DSTstart = dateadd(day, 6 - (datepart(dw, @DSTstart) + @@datefirst - 2) % 7, @DSTstart),
@DSTend = dateadd(day, 6 - (datepart(dw, @DSTend) + @@datefirst - 2) % 7, @DSTend),
@LocalDatetime = case
when frm < til and @LocalDatetime >= @DSTstart and @LocalDatetime < @DSTend -- If it would be DST on the date of the input datetime,
or frm > til and (@LocalDatetime >= @DSTstart or @LocalDatetime < @DSTend) -- add this hour to the input datetime.
then dateadd(hour, 1, @LocalDatetime)
else @LocalDatetime
end
from @calculation
where datediff(minute, getutcdate(), getdate()) between frm * 60 and til * 60
and datepart(year, @LocalDatetime) >= since
order by since desc
return @LocalDatetime
end
Эта функция смотрит на разницу между локальным и утечным временем в моментработает, чтобы определить, какие DST-правила применять.Затем он знает, включает ли в себя datediff(hour, getutcdate(), getdate())
время перехода на летнее время или нет, и вычитает его, если это так.Затем он определяет, был ли он или будет DST на дату входного времени и времени UTC, и, если это так, добавляет время перехода на летнее время.последний час DST и первый час не DST, функция не может определить, какой она есть, и предполагает последний.Таким образом, независимо от ввода-даты-времени, , если этот код запускает в течение последнего часа летнего времени, это даст неверный результат .Это означает, что это работает в 99,9886% случаев.