微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Python自定义假期包括2022年的上一年假期

如何解决Python自定义假期包括2022年的上一年假期

我对类的概念是陌生的,所以请原谅我这个凌乱的代码和暂时缺乏理解。现在,我想我已经弄清楚了如果相关的假期是在周末,那么如何将最近的工作日转变为假期。当我打印假期清单中的内容时,他们通常会打印我期望的内容,除非我尝试2022。我使用以下代码

for day,name in sorted(county_holidays(years = 2022).items()):
    print(day,name)

2019年,2020年和2021年的假期列表看起来不错。但是,2022年的清单包括从上一年的独立日开始的假期。我不知道为什么要这么做。 2023和2024再次看起来不错。这是主要代码

class county_holidays(holidays.HolidayBase):
    def _populate(self,year):

        name = 'New Year\'s Day'
        self[date(year,1,1)] = name
        if date(year,1).weekday() == 5:
            self[date(year,1) + relativedelta(days =- 1)] = name + ' (Observed)'
        if date(year,1).weekday() == 6:
            self[date(year,1) + relativedelta(days =+ 1)] = name + ' (Observed)'
        
        self[date(year,1) + relativedelta(weekday=MO(+3))] = 'Martin Luther King Jr. Day'
        
        name = 'Lincoln\'s Birthday'
        self[date(year,2,12)] = name
        if date(year,12).weekday() == 5:
            self[date(year,12) + relativedelta(days =- 1)] = name + ' (Observed)'
        if date(year,12).weekday() == 6:
            self[date(year,12) + relativedelta(days =+ 1)] = name + ' (Observed)'

        self[date(year,1) + relativedelta(weekday=MO(+3))] = 'Washington\'s Birthday'
        
        name = 'Cesar Chavez Day'
        self[date(year,3,31)] = name
        if date(year,31).weekday() == 5:
            self[date(year,31) + relativedelta(days =- 1)] = name + ' (Observed)'
        if date(year,31).weekday() == 6:
            self[date(year,31) + relativedelta(days =+ 1)] = name + ' (Observed)'
        
        name = 'Memorial Day'
        if (date(year,5,1) + relativedelta(weekday=MO(+5))) > date(year,31):
            self[date(year,1) + relativedelta(weekday=MO(+4))] = name
        else:
            self[date(year,1) + relativedelta(weekday=MO(+5))] = name
        
        name = 'Independence Day'
        self[date(year,7,4)] = name
        if date(year,4).weekday() == 5:
            self[date(year,4) + relativedelta(days =- 1)] = name + ' (Observed)'
        if date(year,4).weekday() == 6:
            self[date(year,4) + relativedelta(days =+ 1)] = name + ' (Observed)'

        self[date(year,9,1) + relativedelta(weekday=MO)] = 'Labor Day'

        self[date(year,10,1) + relativedelta(weekday=MO(+2))] = 'Columbus Day'

        name = 'Veterans Day'
        self[date(year,11,11)] = name
        if date(year,11).weekday() == 5:
            self[date(year,11).weekday() == 6:
            self[date(year,11) + relativedelta(days =+ 1)] = name + ' (Observed)'
        
        self[date(year,1) + relativedelta(weekday = TH(+4))] = 'Thanksgiving Day'

        #self.append({datetime.date(datetime(year,1) + relativedelta(weekday = TH(+4)) + timedelta(days = 1)): 'Day after Thanksgiving'})

        self[date(year,1) + relativedelta(weekday = TH(+4)) + timedelta(days = 1)] = 'Day after Thanksgiving'

        name = 'Christmas Day'
        self[date(year,12,25)] = name
        if date(year,25).weekday() == 5:
            self[date(year,25) + relativedelta(days =- 1)] = name + ' (Observed)'
        if date(year,25).weekday() == 6:
            self[date(year,25) + relativedelta(days =+ 1)] = name + ' (Observed)'

感谢任何关于为什么发生这种情况的指导。在继续添加任何其他内容之前,我想解决该问题。

编辑:在仔细研究了这一点之后,我注意到当元旦是星期六时会发生此错误。它将生成一个假期列表,如下所示:

2022-01-01 New Year's Day
2021-01-01 New Year's Day
2021-01-18 Martin Luther King Jr. Day
...
rest of the holidays for 2021
...
2022-01-17 Martin Luther King Jr. Day
...
rest of the holidays for 2022
...

我仍然不知道会发生什么,因为当其他假期在星期六降临时,不会发生此问题。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。