I came across some dialog where adjustment to daylight savings time was being applied manually in SQL Server stored procedures or functions to calculate the appropriate timezone shift (e.g. sometimes -5 for EST and sometimes -4). I figured it can be done automatically, so I did some research on the definition for Day Light Savings time and came across two rules:
The old rule states:
Starts on the first Sunday of April at 2am and ends on the Last Sunday of October at 2am.
Comparing the results I realized something was off when compared to my current computer clock so I rechecked and found the revised rule enacted a few years ago:
The new rule states:
Starts on the second Sunday of March at 2am and ends on the First Sunday of November at 2am.
declare @currentYear int=datepart(year,getdate())
declare @secondSundayOfMar datetime=CAST('3/8/'+CAST(@currentYear asvarchar)asdatetime)
declare @firstSundayOfNov datetime=CAST('11/1/'+CAST(@currentYear asvarchar)asdatetime)
--find first sunday
while(DATENAME(WEEKDAY,@secondSundayOfMar)!='Sunday')
begin
set @secondSundayOfMar =DATEADD(day,1,@secondSundayOfMar)
end
--find last sunday of october
while(DATENAME(WEEKDAY,@firstSundayOfNov)!='Sunday')
begin
set @firstSundayOfNov =DATEADD(day,-1,@firstSundayOfNov)
end
declare @gmt int= 0
declare @currentDate datetime=getDate()
--for EST
if ( @currentDate >= @secondSundayOfMar AND @currentDate < @firstSundayOfNov )
set @gmt =-4
else
set @gmt =-5
print @gmt