Skip to main content

Timezone conversion UTC to CST with Daylight Savings

Converting UTC format date to Standard time of CST or EST is straight forward.

DECLARE @UTC_Date DATETIME
SET @UTC_Date = GETUTCDATE()
SELECT
@UTC_Date AS [UTC],
DATEADD(hh, -6, @UTC_Date) AS [CST - Standard Time],
DATEADD(hh, -5, @UTC_Date) AS [EST - Standard Time]

But if the given date falls under daylight saving then the above calculation won't work.

So how is Daylight saving calculated?

1. If the year <= 2006 then daylight saving is between:
2 am on First Sunday in April till 2 am on Last Sunday in October

2. If the year >= 2007 then daylight saving is between:
2 am on Second Sunday in March till 2 am on First Sunday in November

3. UTC to CST (Standard Time) = -6
4. UTC to CDT (Daylight Time) = -5

5. UTC to EST (Standard Time) = -5
6. UTC to EDT (DayLight Time) = -4

Solution - 1:

DECLARE @UTC_Date DATETIME
SET @UTC_Date = GETUTCDATE()


SELECT
@UTC_Date AS [UTC],
DATEADD(hh, -6, @UTC_Date) AS [CST - Standard Time],
DATEADD(hh,
CASE WHEN YEAR(@UTC_Date) <= 2006 THEN
                CASE WHEN
                      @UTC_Date >=  '4/' + CAST(ABS(8 - DATEPART(WEEKDAY,'4/1/'
+ CAST(YEAR(@UTC_Date) AS VARCHAR))) % 7 + 1 AS VARCHAR) +  '/'
+ CAST(YEAR(@UTC_Date) AS VARCHAR) + ' 2:00' AND
                      @UTC_Date < '10/' + CAST(32 - DATEPART(WEEKDAY,'10/31/'
+ CAST(YEAR(@UTC_Date) AS VARCHAR)) AS VARCHAR) +  '/'
+ CAST(YEAR(@UTC_Date) AS VARCHAR) + ' 2:00'
                THEN -5 ELSE -6 END
              ELSE
                CASE WHEN
                      @UTC_Date >= '3/' + CAST(ABS(8 - DATEPART(WEEKDAY,'3/1/'
+ CAST(YEAR(@UTC_Date) AS VARCHAR))) % 7 + 8 AS VARCHAR) +  '/'
+ CAST(YEAR(@UTC_Date) AS VARCHAR) + ' 2:00' AND
                      @UTC_Date <
                        '11/' + CAST(ABS(8 - DATEPART(WEEKDAY,'11/1/'
+ CAST(YEAR(@UTC_Date) AS VARCHAR))) % 7 + 1 AS VARCHAR) +  '/'
+ CAST(YEAR(@UTC_Date) AS VARCHAR) + ' 2:00'
                THEN -5 ELSE -6 END
              END
, @UTC_Date
) AS [CST/CDT - DayLight Time],
DATEADD(hh, -5, @UTC_Date) AS [EST - Standard Time],
DATEADD(hh,
CASE WHEN YEAR(@UTC_Date) <= 2006 THEN
                CASE WHEN
                      @UTC_Date >=  '4/' + CAST(ABS(8 - DATEPART(WEEKDAY,'4/1/'
+ CAST(YEAR(@UTC_Date) AS VARCHAR))) % 7 + 1 AS VARCHAR) +  '/'
+ CAST(YEAR(@UTC_Date) AS VARCHAR) + ' 2:00' AND
                      @UTC_Date < '10/' + CAST(32 - DATEPART(WEEKDAY,'10/31/'
+ CAST(YEAR(@UTC_Date) AS VARCHAR)) AS VARCHAR) +  '/'
+ CAST(YEAR(@UTC_Date) AS VARCHAR) + ' 2:00'
                THEN -5 ELSE -6 END
              ELSE
                CASE WHEN
                      @UTC_Date >= '3/' + CAST(ABS(8 - DATEPART(WEEKDAY,'3/1/'
+ CAST(YEAR(@UTC_Date) AS VARCHAR))) % 7 + 8 AS VARCHAR) +  '/'
+ CAST(YEAR(@UTC_Date) AS VARCHAR) + ' 2:00' AND
                      @UTC_Date <
                        '11/' + CAST(ABS(8 - DATEPART(WEEKDAY,'11/1/'
+ CAST(YEAR(@UTC_Date) AS VARCHAR))) % 7 + 1 AS VARCHAR) +  '/'
+ CAST(YEAR(@UTC_Date) AS VARCHAR) + ' 2:00'
                THEN -4 ELSE -5 END
              END
, @UTC_Date
) AS [EST/EDT - DayLight Time]
GO

Instead of calling the same set of code again and again we can make it as a User defined function and call it by passing UTC date, Offset values for standard time and daylight saving respectively.

Solution - 2

/* Parameter 1 = UTC Date
Parameter 2 = Offset value for Standard time
Parameter 3 = Offset value for Daylight saving time */

CREATE FUNCTION [dbo].[fn_GetDaylightSavingsTime]
(
@UTC_Date DATETIME,
@ST_Offset INT, -- CST = -6, EST = -5
@DT_Offset INT  -- CDT = -5, EDT = -4
)
RETURNS DATETIME
AS
BEGIN


RETURN 
DATEADD(hh, 
CASE WHEN YEAR(@UTC_Date) <= 2006 THEN  
                CASE WHEN 
                      @UTC_Date >=  '4/' + CAST(ABS(8 - DATEPART(WEEKDAY,'4/1/' 
+ CAST(YEAR(@UTC_Date) AS VARCHAR))) % 7 + 1 AS VARCHAR) +  '/' 
+ CAST(YEAR(@UTC_Date) AS VARCHAR) + ' 2:00' AND 
                      @UTC_Date < '10/' + CAST(32 - DATEPART(WEEKDAY,'10/31/' 
+ CAST(YEAR(@UTC_Date) AS VARCHAR)) AS VARCHAR) +  '/' 
+ CAST(YEAR(@UTC_Date) AS VARCHAR) + ' 2:00' 
                THEN @DT_Offset ELSE @ST_Offset END
              ELSE
                CASE WHEN 
                      @UTC_Date >= '3/' + CAST(ABS(8 - DATEPART(WEEKDAY,'3/1/' 
+ CAST(YEAR(@UTC_Date) AS VARCHAR))) % 7 + 8 AS VARCHAR) +  '/' 
+ CAST(YEAR(@UTC_Date) AS VARCHAR) + ' 2:00' AND 
                      @UTC_Date < 
                        '11/' + CAST(ABS(8 - DATEPART(WEEKDAY,'11/1/' 
+ CAST(YEAR(@UTC_Date) AS VARCHAR))) % 7 + 1 AS VARCHAR) +  '/' 
+ CAST(YEAR(@UTC_Date) AS VARCHAR) + ' 2:00' 
                THEN @DT_Offset ELSE @ST_Offset END
              END
, @UTC_Date
)
END
GO

Usage:

DECLARE @UTC_Date DATETIME
DECLARE @ST_Offset INT, @DT_Offset INT


SET @UTC_Date = GETUTCDATE()
SET @ST_Offset = -6
SET @DT_Offset = -5


SELECT [dbo].[fn_GetDaylightSavingsTime](@UTC_Date, @ST_Offset, @DT_Offset)

Reference:

Comments

Anonymous said…
Just noticed that you have an error in your EST/EDT calculation in solution 1. In the 2006 and earlier section, you have the time modified by the amounts for central time. The post 2006 section is fine.

Popular posts from this blog

Registry manipulation from SQL

Registry Manupulation from SQL Server is pretty easy. There are 4 extended stored procedure in SQL Server 2000 for the purpose of manupulating the server registry. They are: 1) xp_regwrite 2) xp_regread 3) xp_regdeletekey 4) xp_regdeletevalue Let us see each one of them in detail! About xp_regwrite This extended stored procedure helps us to create data item in the (server’s) registry and we could also create a new key. Usage: We must specify the root key with the @rootkey parameter and an individual key with the @key parameter. Please note that if the key doesn’t exist (without any warnnig) it would be created in the registry. The @value_name parameter designates the data item and the @type the type of the data item. Valid data item types include REG_SZ and REG_DWORD . The last parameter is the @value parameter, which assigns a value to the data item. Let us now see an example which would add a new key called " TestKey ", and a new data item under it called TestKeyValue :

Screen scraping using XmlHttp and Vbscript ...

I wrote a small program for screen scraping any sites using XmlHttp object and VBScript. I know I haven't done any rocket science :) still I thought of sharing the code with you all. XmlHttp -- E x tensible M arkup L anguage H ypertext T ransfer P rotocol An advantage is that - the XmlHttp object queries the server and retrieve the latest information without reloading the page. Source code: < html > < head > < script language ="vbscript"> Dim objXmlHttp Set objXmlHttp = CreateObject("Msxml2.XMLHttp") Function ScreenScrapping() URL == "UR site URL comes here" objXmlHttp.Open "POST", url, False objXmlHttp.onreadystatechange = getref("HandleStateChange") objXmlHttp.Send End Function Function HandleStateChange() If (ObjXmlHttp.readyState = 4) Then msgbox "Screenscrapping completed .." divShowContent.innerHtml = objXmlHttp.responseText End If End Function </ script > < head > < body > &l

Script table as - ALTER TO is greyed out - SQL SERVER

One of my office colleague recently asked me why we are not able to generate ALTER Table script from SSMS. If we right click on the table and choose "Script Table As"  ALTER To option would be disabled or Greyed out. Is it a bug? No it isn't a bug. ALTER To is there to be used for generating modified script of Stored Procedure, Functions, Views, Triggers etc., and NOT for Tables. For generating ALTER Table script there is an work around. Right click on the table, choose "Modify" and enter into the design mode. Make what ever changes you want to make and WITHOUT saving it right click anywhere on the top half of the window (above Column properties) and choose "Generate Change Script". Please be advised that SQL Server would drop actually create a new table with modifications, move the data from the old table into it and then drop the old table. Sounds simple but assume you have a very large table for which you want to do this! Then it woul