March 19
so had to do something interesting for work that I thought would be usefull for others... 2 servers on each coast... need to write the same time, but code does not know which server its running on... so I use gmt to figure out the right time....
heres the solution in sql and c#... sql is useful , as that you could set the default of a datetime field in sql to dbo.fn_GetEastCoastTime(getdate().getutc())
SQL:
-
CREATE FUNCTION [dbo].[fn_GetEastCoastTime]
-
(@date datetime,@utcdate datetime
-
)
-
RETURNS datetime
-
AS
-
BEGIN
-
-
declare @march datetime
-
declare @november datetime
-
declare @dstStart datetime
-
declare @dstEnd datetime
-
declare @difamt int
-
-
SET @march= '3/01/'+cast(year(@date)AS varchar)
-
SET @november= '11/01/'+cast(year(@date)AS varchar)
-
-
SET @dstStart = DATEADD(wk,1,(@march)+(7-DATEPART(dw,(@march-
-
Day(@march)))))
-
-
SET @dstEnd = DATEADD(wk,0,(@november)+(7-DATEPART(dw,(@november-
-
Day(@november)))))
-
-
IF(@date> @dstStart AND @date <@dstEnd)
-
SET @difamt = -4
-
else
-
SET @difamt = -5
-
-
RETURN dateadd(hour,datediff(hour,dateadd(hour,@difamt,@utcdate),@date),@date)
-
-
END
C#:
-
public static DateTime EastCoastDate
-
-
{
-
-
get
-
-
{
-
-
int intTimeShift =-5;
-
-
if(TimeZone.CurrentTimeZone.IsDaylightSavingTime(System.DateTime.Now))
-
-
intTimeShift = -4;
-
-
TimeSpan hourdiff = System.DateTime.UtcNow.AddHours(intTimeShift) - System.DateTime.Now;
-
-
return System.DateTime.Now.AddHours(hourdiff.Hours);
-
-
}
-
-
}
