Detecting a US Holiday

前端 未结 5 1569
醉酒成梦
醉酒成梦 2020-12-25 12:27

What\'s the simplest way to determine if a date is a U.S. bank holiday in Python? There seem to be various calendars and webservices listing holidays for various countries,

5条回答
  •  礼貌的吻别
    2020-12-25 12:58

    Some general comments:

    I don't think that @ast4 really means "nth day of nth week of nth month algorithm". The notion of "nth week in nth month" is mind-snapping (like the "ISO calendar"). I've never seen a holiday defined in terms of "nth week". Martin Luther King Day is an example of the"Nth weekday in month" type of holiday:

    MONDAY, ...., SATURDAY = range(7)
    JAN, ....., DEC = range(1, 12)
    
    Holiday("Martin L King's Birthday", type='floating',
        ordinal=3, weekday=MON, month=JAN)
    Holiday("Memorial Day", type='floating',
        ordinal=-1, weekday=MON, month=MAY)
    

    The USA doesn't have Easter-related holidays. Definition is not difficult:

    Holiday("Good Friday", type='moveable',
        base='gregorian_easter', delta_days=-2)
    Holiday("Easter Monday", etc, delta_days=1)
    # Some states in Australia used to have Easter Tuesday (no kidding)
    Holiday("Easter Tuesday", etc, delta_days=2)
    

    The 'base' idea can be used to cater for lunar new year, in fact any holiday that is an offset from a base date that needs a special procedure to calculate it.

    The so-called "static" holidays are not fixed when the "fixed" date is a Saturday or Sunday and may even vanish (no alternative day off work):

    # Americans will get a day off on Friday 31 Dec 2010
    # because 1 Jan 2011 is a Saturday.
    Holiday("New Year's Day", type='fixed',
        day=1, month=JAN, sat_adj=-1, sun_adj=????)
    
    # Australia observes ANZAC Day on the day, with no day off
    # if the fixed date falls on a weekend.
    Holiday("ANZAC Day", type='fixed', day=25, month=APR, sat_adj=0, sun_adj=0)
    
    # Two consecutive "fixed" holidays is OK; three would need re-thinking.
    # Australia again:
    Holiday("Christmas Day", type='fixed', day=25, month=DEC, sat_adj=2, sun_adj=1)
    Holiday("Boxing Day",    type='fixed', day=26, month=DEC, sat_adj=2, sun_adj=2)
    

    I'm sure there are ways of specifying holidays that aren't catered for by the above rules ... any such news is welcome.

提交回复
热议问题