好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Python3版本下创建计算给定日期范围内工作日方法

眼睛都要挣不开了,代码还有很多需要改进的地方,暂时先把方法放上,明天再整理

 # _*_ coding:utf-8 _*_
# Author: dai bing
# Date: 2020/12/13 22:48
# Scripts: calcworkday.py
# Desc: 给定两个日期区间,计算区间内的工作日
# 需要排除法定节假日以及统计休息日内调整为工作日内的天数
# 给定人数计算出需要多少个任务满足报工需求
#
import datetime
import time

class Tasks:
    weeks = ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期天']
    pattern = "%Y-%m-%d"

    def __init__(self):
        super(Tasks, self).__init__()

        self._person = None
        self._start_date = None
        self._end_date = None
        self._statutory_holidays = None
        self._non_rest_days = None

    @property
    def person(self):
        return self._person

    @person.setter
    def person(self, value):
        self._person = value

    @property
    def startDate(self):
        return self._start_date

    @startDate.setter
    def startDate(self, value):
        sdate = None
        try:
            if isinstance(value, str):
                sdate = datetime.datetime.strptime(value, self.pattern)
            else:
                raise Exception
        except Exception as e:
            print("输入值{}类型为{},需要输入日期型字符串或日期型数组.".format(value, type(value)))
            print(e)
            exit(11)
        self._start_date = sdate

    @property
    def endDate(self):
        return self._end_date

    @endDate.setter
    def endDate(self, value):
        edate = None
        try:
            if isinstance(value, str):
                edate = datetime.datetime.strptime(value, self.pattern)
            else:
                raise Exception
        except Exception as e:
            print("输入值{}类型为{},需要输入日期型字符串或日期型数组.".format(value, type(value)))
            print(e)
            exit(12)
        self._end_date = edate

    @property
    def statutoryHolidays(self):
        return self._statutory_holidays

    @statutoryHolidays.setter
    def statutoryHolidays(self, value):
        change_statutory_holidays = []
        try:
            if isinstance(value, list):
                for sholiday in value:
                    # print(datetime.datetime.strptime(sholiday, pattern))
                    change_statutory_holidays.append(datetime.datetime.strptime(sholiday, self.pattern))
            elif isinstance(value, str):
                change_statutory_holidays.append(datetime.datetime.strptime(value, self.pattern))
        except ValueError:
            print("输入值{}类型为{},需要输入日期型字符串或日期型数组.".format(value, type(value)))
            exit(13)
        self._statutory_holidays = change_statutory_holidays

    @property
    def nonRestDay(self):
        return self._non_rest_days

    @nonRestDay.setter
    def nonRestDay(self, value):
        change_work_day = []
        pattern = "%Y-%m-%d"
        try:
            if isinstance(value, list):
                for non_work_day in value:
                    change_work_day.append(datetime.datetime.strptime(non_work_day, pattern))
            elif isinstance(value, str):
                change_work_day.append(datetime.datetime.strptime(value, pattern))
        except TypeError:
            print("输入值{}类型为{},需要输入日期型字符串或日期型数组.".format(value, type(value)))
            exit(14)
        self._non_rest_days = change_work_day

    # 计算指定日期区间内的工作日
    # statutory_h排除法定节假日
    # work_do休息日调休变更为工作日
    def getWorkDay(self, statutory_h, work_do):
        day_off = 5, 6
        # print(day_off)
        days_work = [x for x in range(7) if x not in day_off]        
        # 排除法定节假日
        statutory_holidays = statutory_h
        # 增加调休工作日
        non_rest_days = work_do
        # print("statutory_holidays: ", len(statutory_holidays))
        # print("work_day_off:", len(work_day_off))
        tag_date = self.startDate
        flag = 0
        while True:
            if tag_date > self.endDate:
                break
            if tag_date.weekday() in days_work and tag_date not in statutory_holidays or tag_date in non_rest_days:
                yield tag_date
            tag_date += datetime.timedelta(days=1)
        # print(days_work)
        # print("flag:", flag)

    def getjobs(self):
        jobs = 1
        coefficient = 1
        while True:
            coefficient = 113 * jobs / int(self.daysCount()) / self.person
            jobs += 1
            # print("coefficient:{},jobs:{}".format(coefficient, jobs))
            if coefficient > 4:
                if coefficient < 5:
                    print("[{}]{}月份共有{}位需要报工人员,需要{}个任务拆分进行报工。"
                          .format(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
                                  datetime.datetime.now().month + 1, self.person, jobs))
                    break

    def daysCount(self):
        """工作日统计,返回数字"""
        return len(list(self.getWorkDay(self.statutoryHolidays, self.nonRestDay)))

if __name__ == "__main__":
    print(__name__)
 

查看更多关于Python3版本下创建计算给定日期范围内工作日方法的详细内容...

  阅读:42次