class TestCalendarWesternWeekWithHolidays(BaseCalendarTest): @classmethod def setup_class(cls): print('\n\nTesting regular week, Mo-Fr, WITH holidays, 2010-2013') warnings.filterwarnings('ignore', module='business_calendar') def __init__(self): BaseCalendarTest.__init__(self) self.holidays = [parse(x) for x in global_holidays.split('\n')] self.cal = Calendar(holidays=self.holidays) self.cal.warn_on_holiday_exhaustion = False rr = rruleset() rr.rrule(rrule(DAILY, byweekday=(MO,TU,WE,TH,FR), dtstart=datetime.datetime(2010,1,1))) for h in self.holidays: rr.exdate(h) self.rr = rr self.dates = rr.between(datetime.datetime(2010,1,1), datetime.datetime(2013,12,31), inc=True) def test_workdaycount_with_non_workday_dates(self): print('test_workdaycount_with_non_workday_dates') """ Jan 1st is a holiday, Jan 30th and 31st fall on a weekend """ daycount = 20; for date2 in ['Jan 30, 2010','Jan 31, 2010','Feb 1, 2010']: count = len(list(self.cal.range('2010-01-01', date2))); assert count == daycount for date2 in ['Jan 29, 2010','Jan 30, 2010','Jan 31, 2010']: count = self.cal.workdaycount('Dec 31, 2009', date2); assert count == daycount+1 for date2 in ['Jan 29, 2010','Jan 30, 2010','Jan 31, 2010']: count = self.cal.busdaycount('Dec 31, 2009', date2); assert count == daycount
class TestCalendarWesternWeek(BaseCalendarTest): @classmethod def setup_class(cls): print('\n\nTesting regular week, Mo-Fr, no holidays, 2010-2013') def __init__(self): BaseCalendarTest.__init__(self) self.cal = Calendar() rr = rruleset() rr.rrule( rrule(DAILY, byweekday=(MO, TU, WE, TH, FR), dtstart=datetime.datetime(2010, 1, 1))) self.rr = rr self.dates = rr.between(datetime.datetime(2010, 1, 1), datetime.datetime(2013, 12, 31), inc=True) def test_workdaycount_with_non_workday_dates(self): print('test_workdaycount_with_non_workday_dates') """ Jan 30th and 31st fall on a weekend """ daycount = 21 for date2 in ['Jan 30, 2010', 'Jan 31, 2010', 'Feb 1, 2010']: count = len(list(self.cal.range('2010-01-01', date2))) assert count == daycount for date2 in ['Jan 29, 2010', 'Jan 30, 2010', 'Jan 31, 2010']: count = self.cal.workdaycount('Dec 31, 2009', date2) assert count == daycount
class TestCalendarWesternWeek(BaseCalendarTest): @classmethod def setup_class(cls): print('\n\nTesting regular week, Mo-Fr, no holidays, 2010-2013') def __init__(self): BaseCalendarTest.__init__(self) self.cal = Calendar() rr = rruleset() rr.rrule(rrule(DAILY, byweekday=(MO,TU,WE,TH,FR), dtstart=datetime.datetime(2010,1,1))) self.rr = rr self.dates = rr.between(datetime.datetime(2010,1,1), datetime.datetime(2013,12,31), inc=True) def test_workdaycount_with_non_workday_dates(self): print('test_workdaycount_with_non_workday_dates') """ Jan 30th and 31st fall on a weekend """ daycount = 21; for date2 in ['Jan 30, 2010','Jan 31, 2010','Feb 1, 2010']: count = len(list(self.cal.range('2010-01-01', date2))); assert count == daycount for date2 in ['Jan 29, 2010','Jan 30, 2010','Jan 31, 2010']: count = self.cal.workdaycount('Dec 31, 2009', date2); assert count == daycount
def test_range(self): busdays = [datetime.date(2018, 4, 28)] cal = Calendar(busdays=busdays) busdays = list( cal.range( datetime.date(2018, 4, 27), datetime.date(2018, 4, 30), )) assert [ datetime.date(2018, 4, 27), datetime.date(2018, 4, 28), ] == busdays
def workdaydic(start, end): "start = '2017/03/17', end = '2017/06/02'" "返回的工作日含start不含end" cal = Calendar() daterange = cal.range(start, end) L = [] [L.append(i) for i in map(lambda x: x.strftime("%Y-%m-%d"), daterange)] DateDictList = OrderedDict() for i in L: if DateDictList.has_key(i) is False: DateDictList[i] = {} DateDictList[i]['Year'] = i.split('-')[0] DateDictList[i]['Month'] = i.split('-')[1] DateDictList[i]['Day'] = i.split('-')[2] else: print 'date dic process completed' return DateDictList
def calc(df, employee_id): myDf = df[df['序号'] == employee_id] cal = Calendar() # TODO 参数外部配置 date1 = datetime(2019, 7, 1) date2 = datetime(2019, 8, 1) # 工作日字符串列表 my_date_key_list = [ date_time.strftime('%Y-%m-%d') for date_time in list(cal.range(date1, date2)) ] # 初始化{工作日:打卡记录}字典 d = {k: [] for k in my_date_key_list} for date_key in my_date_key_list: d[date_key].extend(myDf[myDf['日期'] == date_key]['打卡时间'].tolist()) # 遍历字典判断 weidaka_date_list = [] shangban_weidaka_date_list = [] xiaban_weidaka_date_list = [] chidao_count = 0 chidao_date_list = [] zaotui_count = 0 zaotui_date_list = [] jiaban_count = 0 jiaban_date_list = [] shangban_time = time(9, 0, 0) xiaban_time = time(18, 0, 0) jiaban1_time = time(19, 30, 0) jiaban2_time = time(21, 30, 0) for date_key in my_date_key_list: if len(d[date_key]) == 0: weidaka_date_list.append(date_key) elif len(d[date_key]) == 1: value = d[date_key][0] if shangban_time >= value or (shangban_time < value and value < xiaban_time): xiaban_weidaka_date_list.append(date_key) elif value >= xiaban_time: shangban_weidaka_date_list.append(date_key) if value >= jiaban1_time and value < jiaban2_time: jiaban_count += 1 jiaban_date_list.append(date_key) elif value >= jiaban1_time: jiaban_count += 2 jiaban_date_list.append(date_key) else: starttime = d[date_key][0] endtime = d[date_key][-1] if starttime > shangban_time: chidao_count += 1 chidao_date_list.append(date_key) elif endtime < xiaban_time: zaotui_count += 1 zaotui_date_list.append(date_key) if endtime >= jiaban1_time and endtime < jiaban2_time: jiaban_count += 1 jiaban_date_list.append(date_key) elif endtime >= jiaban1_time: jiaban_count += 2 jiaban_date_list.append(date_key + '*2') print("未打卡:", weidaka_date_list) print("上班未打卡:", shangban_weidaka_date_list) print("下班未打卡:", xiaban_weidaka_date_list) print("迟到次数%s,日期:%s:" % (chidao_count, chidao_date_list)) print("早退次数%s,日期:%s:" % (zaotui_count, zaotui_date_list)) print("加班次数%s,日期:%s:" % (jiaban_count, jiaban_date_list))
class TestCalendarWithSpecialWorkdays: @classmethod def setup_class(cls): print('\n\nTesting Calendar with special workdays') def __init__(self): self.holidays = dict( hworkday=datetime.date(2018, 1, 1), hworkday_two=datetime.date(2018, 1, 2), hweekend_day=datetime.date(2018, 1, 7), ) self.specdays = dict( sholiday=datetime.date(2018, 1, 2), sworkday=datetime.date(2018, 1, 5), ssaturday=datetime.date(2018, 1, 13), ) self.busdays = [datetime.date(2018, 1, 14)] self.cal = Calendar( specdays=self.specdays.values(), holidays=self.holidays.values(), busdays=self.busdays, ) self.cal.warn_on_holiday_exhaustion = False self.workdays = [ datetime.date(2018, 1, i) for i in range(1, 15) if self.cal.isworkday(datetime.date(2018, 1, i)) and not self.cal.isholiday(datetime.date(2018, 1, i)) ] def test_isspecial(self): print('test_special') specdays = self.specdays.values() for sday in specdays: assert self.cal.isspecday(sday) for hday in self.holidays.values(): if hday not in specdays: assert not self.cal.isspecday(hday) def test_special_days_are_also_busdays(self): print('test_special_days_are_also_busdays') for name, day in self.specdays.items(): if not 'workday' in name: assert self.cal.isbusday(day) def test_busdaycount(self): print('test_busdaycount') nbusdays = self.cal.busdaycount( datetime.date(2018, 1, 1), datetime.date(2018, 1, 15), ) assert nbusdays == 12 def test_range(self): print('test_range') busdays = list( self.cal.range( datetime.date(2018, 1, 1), datetime.date(2018, 1, 15), )) check_busdays = [ self.specdays['sholiday'], self.specdays['ssaturday'], self.busdays[0] ] check_busdays.extend(self.workdays) assert sorted(check_busdays) == busdays