Я решил эту проблему для времени Новой Зеландии, как показано ниже:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Vivi Woolford
-- Create date: 27-09-2016
-- Description: This procedure only Works in New Zealand
-- =============================================
CREATE FUNCTION [dbo].[udf_GetLocalTimeFromUTC]
(
@UTCTime DATETIME
)
RETURNS DATETIME
AS
BEGIN
--Daylight Saving commences on the last Sunday in September, when 2.00am becomes 3.00am.
--It ends on the first Sunday in April, when 3.00am becomes 2.00am.
DECLARE @LocalTime DATETIME
DECLARE @OffSet INT = 12
SELECT @LocalTime = DATEADD(HOUR, @OffSet, @UTCTime)
IF @LocalTime BETWEEN
/*FINISH DAY LIGHT SAVINGS*/
DATEADD(HOUR, 2, DATEADD(dd, (6-(DATEDIFF(dd,0,DATEADD(mm,(YEAR(@LocalTime)-1900) * 12 + 3,0))%7)),DATEADD(mm,(YEAR(@LocalTime)-1900) * 12 + 3,0)))
AND
/*START DAY LIGHT SAVINGS*/
DATEADD(HOUR, 2, DATEADD(dd, -1*(DATEPART(dw, DateAdd(day, -1, DateAdd(month, DateDiff(month, 0, @LocalTime)+1, 0)))-1),DateAdd(day, -1, DateAdd(month, DateDiff(month, 0, @LocalTime)+1, 0))))
BEGIN
SELECT @LocalTime = @LocalTime
END
ELSE
BEGIN
SELECT @LocalTime = DATEADD(HOUR, 1, @LocalTime)
END
RETURN @LocalTime
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Vivi Woolford
-- Create date: 27-09-2016
-- Description: This procedure only Works in New Zealand
-- =============================================
ALTER FUNCTION [dbo].[udf_GetUTCFromLocalTime]
(
@LocalTime DATETIME
)
RETURNS DATETIME
AS
BEGIN
--Daylight Saving commences on the last Sunday in September, when 2.00am becomes 3.00am.
--It ends on the first Sunday in April, when 3.00am becomes 2.00am.
DECLARE @UTCTime DATETIME
DECLARE @OffSet INT = 12
IF @LocalTime BETWEEN
/*FINISH DAY LIGHT SAVINGS*/
DATEADD(HOUR, 2, DATEADD(dd, (6-(DATEDIFF(dd,0,DATEADD(mm,(YEAR(@LocalTime)-1900) * 12 + 3,0))%7)),DATEADD(mm,(YEAR(@LocalTime)-1900) * 12 + 3,0)))
AND
/*START DAY LIGHT SAVINGS*/
DATEADD(HOUR, 2, DATEADD(dd, -1*(DATEPART(dw, DateAdd(day, -1, DateAdd(month, DateDiff(month, 0, @LocalTime)+1, 0)))-1),DateAdd(day, -1, DateAdd(month, DateDiff(month, 0, @LocalTime)+1, 0))))
BEGIN
SELECT @UTCTime = DATEADD(HOUR, -@OffSet, @LocalTime)
END
ELSE
BEGIN
SELECT @UTCTime = DATEADD(HOUR, -1-@OffSet, @LocalTime)
END
RETURN @UTCTime
END
go