• 使用python生成工作日和节假日的json数据


    • 写这个的目的是要得到工作日,换休工作日,节假日的json数据,方便公司根据这个数据配置免佣时间,而有的插件又不能及时更新。
    • 每年国家发布节假日日期,就可以自己添加,生成数据了。

    1、配置节假日,换休工作日的数据myHoliday.py:

    from enum import Enum
    import datetime
    class Holiday(Enum):
        def __new__(cls, english, chinese, days):
            obj = object.__new__(cls)
            obj._value_ = english
    
            obj.chinese = chinese
            obj.days = days
            return obj
    
        new_years_day = "New Year's Day", "元旦", 1
        spring_festival = "Spring Festival", "春节", 3
        tomb_sweeping_day = "Tomb-sweeping Day", "清明", 1
        labour_day = "Labour Day", "劳动节", 1
        dragon_boat_festival = "Dragon Boat Festival", "端午", 1
        national_day = "National Day", "国庆节", 3
        mid_autumn_festival = "Mid-autumn Festival", "中秋", 1
    
        # special holidays
        anti_fascist_70th_day = "Anti-Fascist 70th Day", "中国人民抗日战争暨世界反法西斯战争胜利70周年纪念日", 1
    
    
    holidays = {
        datetime.date(year=2018, month=1, day=1): Holiday.new_years_day.value,
        datetime.date(year=2018, month=2, day=15): Holiday.spring_festival.value,
        datetime.date(year=2018, month=2, day=16): Holiday.spring_festival.value,
        datetime.date(year=2018, month=2, day=17): Holiday.spring_festival.value,
        datetime.date(year=2018, month=2, day=18): Holiday.spring_festival.value,
        datetime.date(year=2018, month=2, day=19): Holiday.spring_festival.value,
        datetime.date(year=2018, month=2, day=20): Holiday.spring_festival.value,
        datetime.date(year=2018, month=2, day=21): Holiday.spring_festival.value,
        datetime.date(year=2018, month=4, day=5): Holiday.tomb_sweeping_day.value,
        datetime.date(year=2018, month=4, day=6): Holiday.tomb_sweeping_day.value,
        datetime.date(year=2018, month=4, day=7): Holiday.tomb_sweeping_day.value,
        datetime.date(year=2018, month=4, day=29): Holiday.labour_day.value,
        datetime.date(year=2018, month=4, day=30): Holiday.labour_day.value,
        datetime.date(year=2018, month=5, day=1): Holiday.labour_day.value,
        datetime.date(year=2018, month=6, day=18): Holiday.dragon_boat_festival.value,
        datetime.date(year=2018, month=9, day=24): Holiday.mid_autumn_festival.value,
        datetime.date(year=2018, month=10, day=1): Holiday.national_day.value,
        datetime.date(year=2018, month=10, day=2): Holiday.national_day.value,
        datetime.date(year=2018, month=10, day=3): Holiday.national_day.value,
        datetime.date(year=2018, month=10, day=4): Holiday.national_day.value,
        datetime.date(year=2018, month=10, day=5): Holiday.national_day.value,
        datetime.date(year=2018, month=10, day=6): Holiday.national_day.value,
        datetime.date(year=2018, month=10, day=7): Holiday.national_day.value,
        datetime.date(year=2018, month=12, day=30): Holiday.new_years_day.value,
        datetime.date(year=2018, month=12, day=31): Holiday.new_years_day.value,
        datetime.date(year=2019, month=1, day=1): Holiday.new_years_day.value,
        datetime.date(year=2019, month=2, day=4): Holiday.spring_festival.value,
        datetime.date(year=2019, month=2, day=5): Holiday.spring_festival.value,
        datetime.date(year=2019, month=2, day=6): Holiday.spring_festival.value,
        datetime.date(year=2019, month=2, day=7): Holiday.spring_festival.value,
        datetime.date(year=2019, month=2, day=8): Holiday.spring_festival.value,
        datetime.date(year=2019, month=2, day=9): Holiday.spring_festival.value,
        datetime.date(year=2019, month=2, day=10): Holiday.spring_festival.value,
        datetime.date(year=2019, month=4, day=5): Holiday.tomb_sweeping_day.value,
        datetime.date(year=2019, month=4, day=6): Holiday.tomb_sweeping_day.value,
        datetime.date(year=2019, month=4, day=7): Holiday.tomb_sweeping_day.value,
        datetime.date(year=2019, month=5, day=1): Holiday.labour_day.value,
        datetime.date(year=2019, month=5, day=2): Holiday.labour_day.value,
        datetime.date(year=2019, month=5, day=3): Holiday.labour_day.value,
        datetime.date(year=2019, month=5, day=4): Holiday.labour_day.value,
        datetime.date(year=2019, month=6, day=7): Holiday.dragon_boat_festival.value,
        datetime.date(year=2019, month=6, day=8): Holiday.dragon_boat_festival.value,
        datetime.date(year=2019, month=6, day=9): Holiday.dragon_boat_festival.value,
        datetime.date(year=2019, month=9, day=13): Holiday.mid_autumn_festival.value,
        datetime.date(year=2019, month=9, day=14): Holiday.mid_autumn_festival.value,
        datetime.date(year=2019, month=9, day=15): Holiday.mid_autumn_festival.value,
        datetime.date(year=2019, month=10, day=1): Holiday.national_day.value,
        datetime.date(year=2019, month=10, day=2): Holiday.national_day.value,
        datetime.date(year=2019, month=10, day=3): Holiday.national_day.value,
        datetime.date(year=2019, month=10, day=4): Holiday.national_day.value,
        datetime.date(year=2019, month=10, day=5): Holiday.national_day.value,
        datetime.date(year=2019, month=10, day=6): Holiday.national_day.value,
        datetime.date(year=2019, month=10, day=7): Holiday.national_day.value,
        datetime.date(year=2020, month=1, day=1): Holiday.new_years_day.value,
        datetime.date(year=2020, month=1, day=24): Holiday.spring_festival.value,
        datetime.date(year=2020, month=1, day=25): Holiday.spring_festival.value,
        datetime.date(year=2020, month=1, day=26): Holiday.spring_festival.value,
        datetime.date(year=2020, month=1, day=27): Holiday.spring_festival.value,
        datetime.date(year=2020, month=1, day=28): Holiday.spring_festival.value,
        datetime.date(year=2020, month=1, day=29): Holiday.spring_festival.value,
        datetime.date(year=2020, month=1, day=30): Holiday.spring_festival.value,
        datetime.date(year=2020, month=1, day=31): Holiday.spring_festival.value,
        datetime.date(year=2020, month=2, day=1): Holiday.spring_festival.value,
        datetime.date(year=2020, month=2, day=2): Holiday.spring_festival.value,
        datetime.date(year=2020, month=4, day=4): Holiday.tomb_sweeping_day.value,
        datetime.date(year=2020, month=4, day=5): Holiday.tomb_sweeping_day.value,
        datetime.date(year=2020, month=4, day=6): Holiday.tomb_sweeping_day.value,
        datetime.date(year=2020, month=5, day=1): Holiday.labour_day.value,
        datetime.date(year=2020, month=5, day=2): Holiday.labour_day.value,
        datetime.date(year=2020, month=5, day=3): Holiday.labour_day.value,
        datetime.date(year=2020, month=5, day=4): Holiday.labour_day.value,
        datetime.date(year=2020, month=5, day=5): Holiday.labour_day.value,
        datetime.date(year=2020, month=6, day=25): Holiday.dragon_boat_festival.value,
        datetime.date(year=2020, month=6, day=26): Holiday.dragon_boat_festival.value,
        datetime.date(year=2020, month=6, day=27): Holiday.dragon_boat_festival.value,
        datetime.date(year=2020, month=10, day=1): Holiday.national_day.value,
        datetime.date(year=2020, month=10, day=2): Holiday.national_day.value,
        datetime.date(year=2020, month=10, day=3): Holiday.national_day.value,
        datetime.date(year=2020, month=10, day=4): Holiday.national_day.value,
        datetime.date(year=2020, month=10, day=5): Holiday.national_day.value,
        datetime.date(year=2020, month=10, day=6): Holiday.national_day.value,
        datetime.date(year=2020, month=10, day=7): Holiday.national_day.value,
        datetime.date(year=2020, month=10, day=8): Holiday.national_day.value,
        datetime.date(year=2021, month=1, day=1): Holiday.new_years_day.value,
        datetime.date(year=2021, month=1, day=2): Holiday.new_years_day.value,
        datetime.date(year=2021, month=1, day=3): Holiday.new_years_day.value,
        datetime.date(year=2021, month=2, day=11): Holiday.spring_festival.value,
        datetime.date(year=2021, month=2, day=12): Holiday.spring_festival.value,
        datetime.date(year=2021, month=2, day=13): Holiday.spring_festival.value,
        datetime.date(year=2021, month=2, day=14): Holiday.spring_festival.value,
        datetime.date(year=2021, month=2, day=15): Holiday.spring_festival.value,
        datetime.date(year=2021, month=2, day=16): Holiday.spring_festival.value,
        datetime.date(year=2021, month=2, day=17): Holiday.spring_festival.value,
        datetime.date(year=2021, month=4, day=3): Holiday.tomb_sweeping_day.value,
        datetime.date(year=2021, month=4, day=4): Holiday.tomb_sweeping_day.value,
        datetime.date(year=2021, month=4, day=5): Holiday.tomb_sweeping_day.value,
        datetime.date(year=2021, month=5, day=1): Holiday.labour_day.value,
        datetime.date(year=2021, month=5, day=2): Holiday.labour_day.value,
        datetime.date(year=2021, month=5, day=3): Holiday.labour_day.value,
        datetime.date(year=2021, month=5, day=4): Holiday.labour_day.value,
        datetime.date(year=2021, month=5, day=5): Holiday.labour_day.value,
        datetime.date(year=2021, month=6, day=12): Holiday.dragon_boat_festival.value,
        datetime.date(year=2021, month=6, day=13): Holiday.dragon_boat_festival.value,
        datetime.date(year=2021, month=6, day=14): Holiday.dragon_boat_festival.value,
        datetime.date(year=2021, month=9, day=19): Holiday.mid_autumn_festival.value,
        datetime.date(year=2021, month=9, day=20): Holiday.mid_autumn_festival.value,
        datetime.date(year=2021, month=9, day=21): Holiday.mid_autumn_festival.value,
        datetime.date(year=2021, month=10, day=1): Holiday.national_day.value,
        datetime.date(year=2021, month=10, day=2): Holiday.national_day.value,
        datetime.date(year=2021, month=10, day=3): Holiday.national_day.value,
        datetime.date(year=2021, month=10, day=4): Holiday.national_day.value,
        datetime.date(year=2021, month=10, day=5): Holiday.national_day.value,
        datetime.date(year=2021, month=10, day=6): Holiday.national_day.value,
        datetime.date(year=2021, month=10, day=7): Holiday.national_day.value,
    }
    
    #放假调休工作日
    workdays = {
        datetime.date(year=2004, month=1, day=17): Holiday.spring_festival.value,
        datetime.date(year=2004, month=1, day=18): Holiday.spring_festival.value,
        datetime.date(year=2004, month=5, day=8): Holiday.labour_day.value,
        datetime.date(year=2004, month=5, day=9): Holiday.labour_day.value,
        datetime.date(year=2004, month=10, day=9): Holiday.national_day.value,
        datetime.date(year=2004, month=10, day=10): Holiday.national_day.value,
        datetime.date(year=2005, month=2, day=5): Holiday.spring_festival.value,
        datetime.date(year=2005, month=2, day=6): Holiday.spring_festival.value,
        datetime.date(year=2005, month=4, day=30): Holiday.labour_day.value,
        datetime.date(year=2005, month=5, day=8): Holiday.labour_day.value,
        datetime.date(year=2005, month=10, day=8): Holiday.national_day.value,
        datetime.date(year=2005, month=10, day=9): Holiday.national_day.value,
        datetime.date(year=2006, month=1, day=28): Holiday.spring_festival.value,
        datetime.date(year=2006, month=2, day=5): Holiday.spring_festival.value,
        datetime.date(year=2006, month=4, day=29): Holiday.labour_day.value,
        datetime.date(year=2006, month=4, day=30): Holiday.labour_day.value,
        datetime.date(year=2006, month=9, day=30): Holiday.national_day.value,
        datetime.date(year=2006, month=10, day=8): Holiday.national_day.value,
        datetime.date(year=2006, month=12, day=30): Holiday.new_years_day.value,
        datetime.date(year=2006, month=12, day=31): Holiday.new_years_day.value,
        datetime.date(year=2007, month=2, day=17): Holiday.spring_festival.value,
        datetime.date(year=2007, month=2, day=25): Holiday.spring_festival.value,
        datetime.date(year=2007, month=4, day=28): Holiday.labour_day.value,
        datetime.date(year=2007, month=4, day=29): Holiday.labour_day.value,
        datetime.date(year=2007, month=9, day=29): Holiday.national_day.value,
        datetime.date(year=2007, month=9, day=30): Holiday.national_day.value,
        datetime.date(year=2008, month=2, day=2): Holiday.spring_festival.value,
        datetime.date(year=2008, month=2, day=3): Holiday.spring_festival.value,
        datetime.date(year=2008, month=5, day=4): Holiday.labour_day.value,
        datetime.date(year=2009, month=1, day=4): Holiday.new_years_day.value,
        datetime.date(year=2009, month=1, day=24): Holiday.spring_festival.value,
        datetime.date(year=2009, month=2, day=1): Holiday.spring_festival.value,
        datetime.date(year=2009, month=5, day=31): Holiday.dragon_boat_festival.value,
        datetime.date(year=2009, month=9, day=27): Holiday.national_day.value,
        datetime.date(year=2009, month=10, day=10): Holiday.national_day.value,
        datetime.date(year=2010, month=2, day=20): Holiday.spring_festival.value,
        datetime.date(year=2010, month=2, day=21): Holiday.spring_festival.value,
        datetime.date(year=2010, month=6, day=12): Holiday.dragon_boat_festival.value,
        datetime.date(year=2010, month=6, day=13): Holiday.dragon_boat_festival.value,
        datetime.date(year=2010, month=9, day=19): Holiday.mid_autumn_festival.value,
        datetime.date(year=2010, month=9, day=25): Holiday.mid_autumn_festival.value,
        datetime.date(year=2010, month=9, day=26): Holiday.national_day.value,
        datetime.date(year=2010, month=10, day=9): Holiday.national_day.value,
        datetime.date(year=2011, month=1, day=30): Holiday.spring_festival.value,
        datetime.date(year=2011, month=2, day=12): Holiday.spring_festival.value,
        datetime.date(year=2011, month=4, day=2): Holiday.tomb_sweeping_day.value,
        datetime.date(year=2011, month=10, day=8): Holiday.national_day.value,
        datetime.date(year=2011, month=10, day=9): Holiday.national_day.value,
        datetime.date(year=2011, month=12, day=31): Holiday.new_years_day.value,
        datetime.date(year=2012, month=1, day=21): Holiday.spring_festival.value,
        datetime.date(year=2012, month=1, day=29): Holiday.spring_festival.value,
        datetime.date(year=2012, month=3, day=31): Holiday.tomb_sweeping_day.value,
        datetime.date(year=2012, month=4, day=1): Holiday.tomb_sweeping_day.value,
        datetime.date(year=2012, month=4, day=28): Holiday.labour_day.value,
        datetime.date(year=2012, month=9, day=29): Holiday.national_day.value,
        datetime.date(year=2013, month=1, day=5): Holiday.new_years_day.value,
        datetime.date(year=2013, month=1, day=6): Holiday.new_years_day.value,
        datetime.date(year=2013, month=2, day=16): Holiday.spring_festival.value,
        datetime.date(year=2013, month=2, day=17): Holiday.spring_festival.value,
        datetime.date(year=2013, month=4, day=7): Holiday.tomb_sweeping_day.value,
        datetime.date(year=2013, month=4, day=27): Holiday.labour_day.value,
        datetime.date(year=2013, month=4, day=28): Holiday.labour_day.value,
        datetime.date(year=2013, month=6, day=8): Holiday.dragon_boat_festival.value,
        datetime.date(year=2013, month=6, day=9): Holiday.dragon_boat_festival.value,
        datetime.date(year=2013, month=9, day=22): Holiday.mid_autumn_festival.value,
        datetime.date(year=2013, month=9, day=29): Holiday.national_day.value,
        datetime.date(year=2013, month=10, day=12): Holiday.national_day.value,
        datetime.date(year=2014, month=1, day=26): Holiday.spring_festival.value,
        datetime.date(year=2014, month=2, day=8): Holiday.spring_festival.value,
        datetime.date(year=2014, month=5, day=4): Holiday.labour_day.value,
        datetime.date(year=2014, month=9, day=28): Holiday.national_day.value,
        datetime.date(year=2014, month=10, day=11): Holiday.national_day.value,
        datetime.date(year=2015, month=1, day=4): Holiday.new_years_day.value,
        datetime.date(year=2015, month=2, day=15): Holiday.spring_festival.value,
        datetime.date(year=2015, month=2, day=28): Holiday.spring_festival.value,
        datetime.date(year=2015, month=9, day=6): Holiday.anti_fascist_70th_day.value,
        datetime.date(year=2015, month=10, day=10): Holiday.national_day.value,
        datetime.date(year=2016, month=2, day=6): Holiday.spring_festival.value,
        datetime.date(year=2016, month=2, day=14): Holiday.spring_festival.value,
        datetime.date(year=2016, month=6, day=12): Holiday.dragon_boat_festival.value,
        datetime.date(year=2016, month=9, day=18): Holiday.mid_autumn_festival.value,
        datetime.date(year=2016, month=10, day=8): Holiday.national_day.value,
        datetime.date(year=2016, month=10, day=9): Holiday.national_day.value,
        datetime.date(year=2017, month=1, day=22): Holiday.spring_festival.value,
        datetime.date(year=2017, month=2, day=4): Holiday.spring_festival.value,
        datetime.date(year=2017, month=4, day=1): Holiday.tomb_sweeping_day.value,
        datetime.date(year=2017, month=5, day=27): Holiday.dragon_boat_festival.value,
        datetime.date(year=2017, month=9, day=30): Holiday.national_day.value,
        datetime.date(year=2018, month=2, day=11): Holiday.spring_festival.value,
        datetime.date(year=2018, month=2, day=24): Holiday.spring_festival.value,
        datetime.date(year=2018, month=4, day=8): Holiday.tomb_sweeping_day.value,
        datetime.date(year=2018, month=4, day=28): Holiday.labour_day.value,
        datetime.date(year=2018, month=9, day=29): Holiday.national_day.value,
        datetime.date(year=2018, month=9, day=30): Holiday.national_day.value,
        datetime.date(year=2018, month=12, day=29): Holiday.new_years_day.value,
        datetime.date(year=2019, month=2, day=2): Holiday.spring_festival.value,
        datetime.date(year=2019, month=2, day=3): Holiday.spring_festival.value,
        datetime.date(year=2019, month=4, day=28): Holiday.labour_day.value,
        datetime.date(year=2019, month=5, day=5): Holiday.labour_day.value,
        datetime.date(year=2019, month=9, day=29): Holiday.national_day.value,
        datetime.date(year=2019, month=10, day=12): Holiday.national_day.value,
        datetime.date(year=2020, month=1, day=19): Holiday.spring_festival.value,
        datetime.date(year=2020, month=4, day=26): Holiday.labour_day.value,
        datetime.date(year=2020, month=5, day=9): Holiday.labour_day.value,
        datetime.date(year=2020, month=6, day=28): Holiday.dragon_boat_festival.value,
        datetime.date(year=2020, month=9, day=27): Holiday.national_day.value,
        datetime.date(year=2020, month=10, day=10): Holiday.national_day.value,
        datetime.date(year=2021, month=2, day=7): Holiday.spring_festival.value,
        datetime.date(year=2021, month=2, day=20): Holiday.spring_festival.value,
        datetime.date(year=2021, month=4, day=25): Holiday.labour_day.value,
        datetime.date(year=2021, month=5, day=8): Holiday.labour_day.value,
        datetime.date(year=2021, month=9, day=18): Holiday.mid_autumn_festival.value,
        datetime.date(year=2021, month=9, day=26): Holiday.national_day.value,
        datetime.date(year=2021, month=10, day=9): Holiday.national_day.value,
    }
    每年节假日和因节假日换休工作日

     2、生成json文件(其中:0表示工作日和因节假日换休的工作日,1表示星期六和星期天(因节假日换休的工作日的星期六和星期天不算1算0),2表示节假日):

    import calendar
    import datetime
    from myHoliday import holidays ,workdays
    
    path = 'C:/Users/Administrator/Desktop/'+'calendar'+'.json'
    file = open(path,'w')
    
    startMessage='{
    '
    endMessage = '}'
    year = 2021
    mouths = [1,2,3,4,5,6,7,8,9,10,11,12]
    file.write(startMessage)
    
    
    for mouth in mouths:
        monthDays = calendar.monthrange(year,mouth)
        for day in (range(1,monthDays[1]+1,1)):
            dayObject = datetime.date(year, mouth, day)
            dayString = str(dayObject).replace("-","")
            dayType = 0
            #判断是否为工作日(星期一至星期五)
            if(calendar.weekday(year, mouth, day)>4):
                dayType = 1
            if(dayObject in holidays.keys()):
                dayType = 2
            #判断是不是节假日调休的上班工作日
            if(dayObject in workdays.keys()):
                dayType = 0
            file.write('	"%s":%d,
    '%(dayString, dayType))
    file.write(endMessage)
    file.close

     3、效果(最后的一个数据逗号去掉):

  • 相关阅读:
    动态数据源切换
    Disconf实践指南:改造篇
    Disconf实践指南:使用篇
    Disconf实践指南:安装篇
    执行Git命令时出现各种 SSL certificate problem 的解决办法
    linux rz 乱码
    分布式配置项管理-开源方案预研究
    mac下mysql5.7.18修改root密码
    git 版本回滚
    关于@Autowired使用注意点
  • 原文地址:https://www.cnblogs.com/dupenghui/p/15218767.html
Copyright © 2020-2023  润新知