Torna al Thread
ALTER function [dbo].[GetLocalFromUTC](@DTFROM datetime)
returns datetime
as
begin
declare @LocalTime datetime, @Offset int
set @LocalTime=@DTFROM
set @DTFROM=getdate()
Declare @Apr varchar(5), @Oct varchar(5), @Spring_date datetime, @Fall_date datetime, @Mar varchar(5), @Nov varchar(5)
if year(@DTFROM) <=2006
begin
set @Apr = (2 + 6 * year(@DTFROM) - (floor(year(@DTFROM) / 4))) % 7 + 1
set @Oct = (31 - ((year(@DTFROM) * 5 / 4) + 1) % 7)
set @Spring_date = '4/' + @Apr + '/' + convert(char(4),year(@DTFROM))
set @Fall_date = '10/' + @Oct + '/' + convert(char(4),year(@DTFROM))
end
else
begin
set @Mar = 14 - (floor (1 + year(@DTFROM) * 5 / 4) % 7);
set @Nov = 7 - (floor (1 + year(@DTFROM) * 5 / 4) % 7);
set @Spring_date = '3/' + @Mar + '/' + convert(char(4),year(@DTFROM))
set @Fall_date = '11/' + @Nov + '/' + convert(char(4),year(@DTFROM))
end
--select @Spring_date as SpringDate, @Fall_date as FallDate
set @offset=datediff(hh, getutcdate(), getdate())
if @DTFROM between @Spring_date and @Fall_date
set @Offset=@Offset-1
set @DTFROM=@LocalTime
if year(@DTFROM) <=2006
begin
set @Apr = (2 + 6 * year(@DTFROM) - (floor(year(@DTFROM) / 4))) % 7 + 1
set @Oct = (31 - ((year(@DTFROM) * 5 / 4) + 1) % 7)
set @Spring_date = '4/' + @Apr + '/' + convert(char(4),year(@DTFROM))
set @Fall_date = '10/' + @Oct + '/' + convert(char(4),year(@DTFROM))
end
else
begin
set @Mar = 14 - (floor (1 + year(@DTFROM) * 5 / 4) % 7);
set @Nov = 7 - (floor (1 + year(@DTFROM) * 5 / 4) % 7);
set @Spring_date = '3/' + @Mar + '/' + convert(char(4),year(@DTFROM))
set @Fall_date = '11/' + @Nov + '/' + convert(char(4),year(@DTFROM))
end
--select @Spring_date as SpringDate, @Fall_date as FallDate
if @DTFROM between @Spring_date and @Fall_date
set @LocalTime=dateadd(hh,@offset+1,@LocalTime)
else
set @LocalTime=dateadd(hh,@offset,@LocalTime)
return @LocalTime
end