Calculating daylight saving time from only date

后端 未结 9 1600
春和景丽
春和景丽 2021-01-31 10:53

I am working with an Arduino and a real time clock chip. The chip compensates for leap years and such, so it will always have the correct date, but it does not handle daylight s

9条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-31 11:08

    March 14th and November 7 are always part of the week that the day light savings occur in the united states.. they may be the Sunday or the Saturday or and day of the week in-between but they are always part of that week. the code below will find the Sunday of that those two dates are part of in the year that the date in question occurs in. it then adds 2 hours to this date to get the time in which the daylight savings actually occur. you then compare the date in question against the daylight savings beginning and end dates and adjust the time by the gmt offset. This process can work for other countries begin and end dates. you could setup a table that has every country code and postal code in it with daylight savings end and begin dates and the gmt offset for both periods. the dates would be the 7th, 14th, 21th, and 28th, for the first through forth Sundays of a month. you would put in the max day in for the last Sunday of the month ex Sep 30th or Oct 31st.

    2nd Sunday in March:

    cdate("3/14/" & format(now(),"yyyy"))-format(cdate("3/14/" & format(now(),"yyyy")),"W")+1+2/24
    

    1st Sunday November:

    cdate("11/7/" & format(now(),"yyyy"))-format(cdate("11/7/" & format(now(),"yyyy")),"W")+1+2/24
    

    Ex.

    If(now() < cdate("3/14/" & format(now(),"yyyy"))-format(cdate("3/14/" & format(now(),"yyyy")),"W")+1+2/24, dateadd("H",-5,now()), if(now() < cdate("11/7/" & format(now(),"yyyy"))-format(cdate("11/7/" & format(now(),"yyyy")),"W")+1+2/24, dateadd("H",-6,now()), dateadd("H",-5,now())))
    

    t_SQL example

    CASE WHEN [date2check] < DATEADD(hh, 2, CAST('3/14/' + CAST(DATEPART(yyyy, [date2check]) AS nvarchar(4)) AS datetime) + 1 - DATEPART(w, CAST('3/14/' + CAST(DATEPART(yyyy, [date2check]) AS nvarchar(4)) AS datetime))) THEN dateadd(hh, - DST_GMT_TM_ZN_DIFF, [date2check]) ELSE CASE WHEN [date2check] < DATEADD(hh, 2, CAST('11/7/' + CAST(DATEPART(yyyy, [date2check]) AS nvarchar(4)) AS datetime) + 1 - DATEPART(w, CAST('11/7/' + CAST(DATEPART(yyyy, [date2check]) AS nvarchar(4)) AS datetime))) THEN dateadd(hh, - STD_GMT_TM_ZN_DIFF, [date2check]) ELSE dateadd(hh, - DST_GMT_TM_ZN_DIFF, [date2check]) END END

提交回复
热议问题