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

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

AWS fatal error: An error occurred (400) when calling the HeadObject operation: Bad Request

While using AWS and trying to copy a file from a S3 bucket to my EC2 instance ended up with this error message. Command Used: aws s3 cp s3://mybucketname/myfilename.html /var/www/html/ Error: fatal error: An error occurred (400) when calling the HeadObject operation: Bad Request The error goes off if we add the region information to the command statement. I am using Asia Pacific (Mumbai) so used ap-south-1 as the region name. Modified Command: aws s3 cp s3://mybucketname/myfilename.html /var/www/html/ --region ap-south-1

[Non Tech] Want to know the recipe for Omelette :)

Fed up with Bread - Jam and Curd Rice, today i wanted to eat Omelette. Interesting part is I wanted to cook it myself :) So in the first picture you see all the items which are needed for preparing an Omelette. When I had a closer look at the eggs I see that almost all the eggs are broken. But believe me when I bought it couple of days back it was in perfect condition! I was wondering whether the eggs have become rotten or pretty old to consume! I tried taking an egg and break it but couldn't break it at all :) Since I have kept in the freezer all the eggs have frozen and looked like a iron ball :) After trying for few minutes of trying i removed the shell of the egg and then kept that iron ball :) into a bowl and placed it within Oven. I heated it for 1 minute and checked. It melted only to a limit. So i just set it for another 2 minutes and checked it later. It has melted but the part of the egg white has become a Omelette :( I didn't leave it there. I took the bowl out of