1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
CREATE FUNCTION [dbo].[fx_get_utc] ( @Value DATETIME --Should be in Eastern Time. ) RETURNS DATETIME AS BEGIN --This only applies to Eastern Time. --DST starts second Sunday of March at 2:00 AM --DST ends first Sunday of November at 2:00 AM DECLARE @DSTStart DATETIME --This is for Eastern Time. DECLARE @DSTEnd DATETIME --This is for Eastern Time. DECLARE @Offset INT SET @Offset = 0 SET @DSTStart = GETDATE() SET @DSTEnd = @DSTStart --It is March IF DATEPART(MONTH,GETDATE()) = 3 BEGIN --It is Sunday IF DATEPART(WEEKDAY,GETDATE()) = 1 BEGIN --It is second Sunday IF DATEPART(DAY,GETDATE()) >= 8 AND DATEPART(DAY,GETDATE()) <= 14 BEGIN SET @DSTStart = CONVERT(VARCHAR(10), 101) + ' 2:00 AM' END END END --It is November IF DATEPART(MONTH,GETDATE()) = 11 BEGIN --It is sunday IF DATEPART(WEEKDAY,GETDATE()) = 1 BEGIN --It is first Sunday IF DATEPART(DAY,GETDATE()) >= 1 AND DATEPART(DAY,GETDATE()) <= 7 BEGIN SET @DSTEnd = CONVERT(VARCHAR(10), 101) + ' 2:00 AM' END END END IF (GETDATE() >= @DSTStart AND GETDATE() < @DSTEnd) AND @DSTStart <> @DSTEnd BEGIN SET @Offset = 60 END DECLARE @UTCDate DATETIME SET @UTCDate = DATEADD(MINUTE, @Offset + DATEDIFF(MINUTE, GETDATE(), GETUTCDATE()), @Value) RETURN @UTCDate END |
1 2 |
SELECT dbo.fx_get_utc('2/9/2015 4:30 PM') --RIGHT result!!! SELECT DATEADD(second, DATEDIFF(second, GETDATE(), GETUTCDATE()), '2/9/2015 4:30 PM') --WRONG result!!! |
No comments:
Post a Comment