Calculating daylight saving time from only date

后端 未结 9 1689
春和景丽
春和景丽 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:03

    If anyone is looking for this in python, there is a great article here: http://code.activestate.com/recipes/425607-findng-the-xth-day-in-a-month/

    It contains the following code:

    from calendar import monthrange
    
    def dow_date_finder(which_weekday_in_month=FIRST,day=MONDAY,month=JANUARY,year=2000):
        bom, days = monthrange(year, month)
        firstmatch = (day - bom) % 7 + 1
        return xrange(firstmatch, days+1, 7)[which_weekday_in_month]
    

    Keep in mind that you need to have all of the variables present from the top of the page at the link above to make it more user-friendly.

    The below is good for any year 2007 or later:

    def find_dt_of_daylight_savings_time(yr=date.today().year):
        dst = {}
        spring = dow_date_finder(SECOND, SUNDAY, MARCH, yr)
        spring_dt = datetime(int(yr), 3, spring, 3, 0)
        dst['spring'] = spring_dt
        fall = dow_date_finder(FIRST, SUNDAY, NOVEMBER, yr)
        fall_dt = datetime(int(yr), 11, fall, 3, 0)
        dst['fall'] = fall_dt
        return dst
    

提交回复
热议问题