當(dāng)前位置:
首頁 >
工时计算程序
發(fā)布時(shí)間:2024/1/1
41
豆豆
工時(shí)計(jì)算
# coding=utf-8 import numpy as np import pandas as pd import datetime import collections# 計(jì)算時(shí)間差函數(shù) def difftime(click_in, click_out):# 輸入的日期和時(shí)間是字符串形式,需要先將字符串格式化為datetime形式。time1 = datetime.datetime.strptime(click_in, "%H:%M")time2 = datetime.datetime.strptime(click_out, "%H:%M")num = (time2-time1).seconds/60return num# 得到每一個(gè)姓名下所有數(shù)據(jù)的行所引 def checkId(target, a):b = []for index, nums in enumerate(a):if nums == target:b.append(index)return (b)in_path = "20210501-20210531_1(無單多次打卡數(shù)據(jù)).xlsx" df = pd.read_excel(in_path, skiprows=0, header=0) print(df) # 提取工時(shí)計(jì)算所需數(shù)據(jù) Name = df["姓名"].astype("str") Company = df["部門"].astype("str") Date_attendance = df["考勤日期"].astype("str") Clock_time = df["打卡時(shí)間"].astype("str") Group_attendance = df["考勤組"].astype("str") Clock_result = df["打卡結(jié)果"].astype("str")""" # 計(jì)算該公司內(nèi)每個(gè)員工的,0.工作日正常小時(shí)、1.工作日加班小時(shí)、2.雙休日加班小時(shí)、3.法定節(jié)假日加班小時(shí) """ Every_Name_company = [item for item, count in collections.Counter(Name).items() if count >= 1] # 遍歷每個(gè)員工,利用相應(yīng)數(shù)據(jù)計(jì)算出四個(gè)工時(shí) work_time_total_list_all = [] overtime_workday_total_list_all = [] weekend_overtime_workday_list_all = [] holiday_overtime_workday_list = [] Company_Every_person_list = [] department_Every_person_list = [] workday_numbers_list = [] overtime_workday_days_list = [] weekend_overtime_days_list = [] holiday_overtime_workday_list_all = [] # global weekend_overtime_days for i in Every_Name_company:print(i)# 這個(gè)人考勤日期、打卡時(shí)間的索引Date_index_Every_person = checkId(i, Name)# 取出每個(gè)人的考勤日期、打卡時(shí)間數(shù)據(jù)Date_attendance_Every_person = Date_attendance[Date_index_Every_person]Clock_time_Every_person = Clock_time[Date_index_Every_person]# 數(shù)據(jù)劃分。三類數(shù)據(jù),周一-周五:工作日,周六、周日:雙休日,法定節(jié)假日(待實(shí)現(xiàn),周主任要公司年歷,調(diào)休規(guī)則),以考勤日期進(jìn)行篩選。weekend_index = []weekend_index_all = []work_index = []work_index_special = []work_index_all = []holiday_index = []holiday_index_all = []for D in Date_attendance_Every_person:holiday_str_list = ["21-05-01"]if any(key in str(D) for key in holiday_str_list):holiday_index = Date_attendance_Every_person[(Date_attendance_Every_person == str(D))].index.tolist()# 2月份調(diào)休規(guī)則:21-02-06和21-02-07為工作日elif "星期六" in str(D) or "星期日" in str(D) or "21-05-03" in str(D) or "21-05-04" in str(D) or "21-05-05" in str(D):if "21-05-08" not in str(D):weekend_index = Date_attendance_Every_person[(Date_attendance_Every_person == str(D))].index.tolist()else:work_index_special = Date_attendance_Every_person[(Date_attendance_Every_person == str(D))].index.tolist()else:work_index = Date_attendance_Every_person[(Date_attendance_Every_person == str(D))].index.tolist()# 工作日索引work_index_all = work_index_all + work_index + work_index_special# 周六日索引weekend_index_all = weekend_index_all + weekend_index# 節(jié)假日索引holiday_index_all = holiday_index_all + holiday_index# 剔除正常工作日、雙休日和節(jié)假日重復(fù)索引work_index_all = sorted(list(set(work_index_all)))weekend_index_all = sorted(list(set(weekend_index_all)))holiday_index_all = sorted(list(set(holiday_index_all)))print("工作日索引", work_index_all)print("雙休日索引", weekend_index_all)print("法定假日索引", holiday_index_all)# 計(jì)算工時(shí)"""# 這個(gè)人工作日每天正常小時(shí)計(jì)算,周一至周五"""# 工作日正常工作work_time_std_1 = []work_time_std_2 = []work_time_early = []work_time_late = []late_early_time = []overtime_workday = []# 工作日工時(shí)Clock_time_Every_person_work = Clock_time[work_index_all]# 每次取出連續(xù)兩行數(shù)據(jù),每次拿出每個(gè)人一天的打卡時(shí)間workday_numbers = 0overtime_workday_days = 0for j in [Clock_time_Every_person_work[k: k + 2] for k in range(0, len(Clock_time_Every_person_work), 2)]:clock_in_time = str(j.iloc[0])clock_out_time = str(j.iloc[-1])# 字符串切片,為了比較,只要時(shí)間字符串,不要日期字符clock_in_time = clock_in_time[11: 16]clock_out_time = clock_out_time[11: 16]# 工作日正常小時(shí)計(jì)算clock_in_std_time = "08:30"clock_out_std_time = "18:00"# 如果上班打卡早于8:30,下班打卡晚于18:00,這一天工作日正常小時(shí)為8小時(shí)if clock_in_time.__gt__(clock_in_std_time) is False and clock_out_time.__gt__(clock_out_std_time) is True:work_time_std_1 = work_time_std_1 + [8 * 60]# 另外一種正常情況,如果上班早于9點(diǎn),下班早于18點(diǎn),如果下班早于18點(diǎn),彈性工作elif clock_in_time.__gt__(clock_in_std_time) is False and clock_out_time.__gt__(clock_out_std_time) is False:if (difftime(clock_in_time, clock_in_std_time) - difftime(clock_out_time, clock_out_std_time)) > 0:work_time_std_2 = work_time_std_2 + [8 * 60]else:early_time = difftime(clock_out_time, clock_out_std_time) - difftime(clock_in_time, clock_in_std_time)# 新的算法early_std_time = np.linspace(0.5, 8, 16)for k in early_std_time:if early_time < k * 60 and early_time > (k - 0.5) * 60:work_time_early = work_time_early + [8 * 60 - k * 60]# 遲到情況工時(shí)計(jì)算elif clock_in_time.__gt__(clock_in_std_time) is True and clock_out_time.__gt__(clock_out_std_time) is True:# 遲到時(shí)間late_time = difftime(clock_in_std_time, clock_in_time)late_std_time = np.linspace(0.5, 8, 16)for k in late_std_time:if late_time <= k * 60 and late_time > (k - 0.5) * 60:work_time_late = work_time_late + [8 * 60 - k * 60]else:late_time_1 = difftime(clock_in_std_time, clock_in_time)earlt_time_1 = difftime(clock_out_time, clock_out_std_time)late_early_time_1 = late_time_1 + earlt_time_1late_early_1_std_time = np.linspace(0.5, 8, 16)for k in late_early_1_std_time:if late_early_time_1 <= k * 60 and late_early_time_1 > (k - 0.5) * 60:late_early_time = late_early_time + [8 * 60 - k * 60]# 計(jì)算工作日加班時(shí)間if clock_out_time.__gt__(clock_out_std_time) is True:if difftime(clock_out_std_time, clock_out_time) >= 60:overtime_workday = overtime_workday + [difftime(clock_out_std_time, clock_out_time) - 30]overtime_workday_days = overtime_workday_days + 1# 計(jì)算這個(gè)人的工作日天數(shù)workday_numbers = workday_numbers + 1# 計(jì)算雙休日加班時(shí)間weekend_overtime_workday_list = []if len(weekend_index_all) == 0:weekend_overtime_workday_list = weekend_overtime_workday_list + [0]else:Clock_time_Every_person_overtime_workday = Clock_time[weekend_index_all]weekend_overtime_workday = []weekend_overtime_days = 0for r in [Clock_time_Every_person_overtime_workday[p: p + 2] for p in range(0, len(Clock_time_Every_person_overtime_workday), 2)]:weekend_clock_in_time = str(r.iloc[0])weekend_clock_out_time = str(r.iloc[-1])# 字符串切片,為了比較,只要時(shí)間字符串,不要日期字符weekend_clock_in_time = weekend_clock_in_time[11: 16]weekend_clock_out_time = weekend_clock_out_time[11: 16]print(weekend_clock_in_time)print(weekend_clock_out_time)if difftime(weekend_clock_in_time, weekend_clock_out_time) < 90:weekend_overtime_workday_list = weekend_overtime_workday_list + [0]else:# 減90是中午吃飯時(shí)間weekend_overtime_workday_list = weekend_overtime_workday_list + [difftime(weekend_clock_in_time,weekend_clock_out_time) - 90]weekend_overtime_days = weekend_overtime_days + 1# 計(jì)算法定節(jié)假日加班時(shí)間,與周六日加班時(shí)間如出一轍holiday_overtime_workday_list = []if len(holiday_index_all) == 0:holiday_overtime_workday_list = holiday_overtime_workday_list + [0]else:Clock_time_Every_person_overtime_holiday = Clock_time[holiday_index_all]holiday_overtime_workday = []# holiday_overtime_days = 0for r in [Clock_time_Every_person_overtime_holiday[p: p + 2] for p inrange(0, len(Clock_time_Every_person_overtime_holiday), 2)]:holiday_clock_in_time = str(r.iloc[0])holiday_clock_out_time = str(r.iloc[-1])# 字符串切片,為了比較,只要時(shí)間字符串,不要日期字符holiday_clock_in_time = holiday_clock_in_time[11: 16]holiday_clock_out_time = holiday_clock_out_time[11: 16]if difftime(holiday_clock_in_time, holiday_clock_out_time) < 90:holiday_overtime_workday_list = holiday_overtime_workday_list + [0]else:# 減90是中午吃飯時(shí)間holiday_overtime_workday_list = holiday_overtime_workday_list + [difftime(holiday_clock_in_time, holiday_clock_out_time) - 90]# holiday_overtime_days = holiday_overtime_days + 1# 匯 總 生成各種工時(shí)統(tǒng)計(jì)列表work_time_total_list = work_time_std_1 + work_time_std_2 + work_time_early + work_time_late + late_early_time# 每個(gè)人的工作日正常小時(shí)work_time_total = round(sum(work_time_total_list) / 60)work_time_total_list_all.append(work_time_total)print("工作日工時(shí)", work_time_total)# 每個(gè)人工作日加班小時(shí)overtime_workday_total = round(sum(overtime_workday) / 60)overtime_workday_total_list_all.append(overtime_workday_total)print("工作日加班小時(shí)", overtime_workday_total)# 每個(gè)人雙休日加班小時(shí)weekend_overtime_workday_list = round(sum(weekend_overtime_workday_list) / 60)weekend_overtime_workday_list_all.append(weekend_overtime_workday_list)print("雙休日加班小時(shí)", weekend_overtime_workday_list)holiday_overtime_workday_list = round(sum(holiday_overtime_workday_list) / 60)holiday_overtime_workday_list_all.append(holiday_overtime_workday_list)print("法定節(jié)假日加班小時(shí)", holiday_overtime_workday_list)# 每個(gè)人對(duì)應(yīng)公司Company_Every_person = list(set(Company[Date_index_Every_person].tolist()))Company_Every_person = " ".join(Company_Every_person)department_Every_person = list(set(Group_attendance[Date_index_Every_person].tolist()))department_Every_person = " ".join(department_Every_person)Company_Every_person_list.append(Company_Every_person)department_Every_person_list.append(department_Every_person)# 天數(shù)workday_numbers_list.append(workday_numbers)overtime_workday_days_list.append(overtime_workday_days)# weekend_overtime_days_list.append(weekend_overtime_days_1)print(workday_numbers_list)print(overtime_workday_days_list)print(overtime_workday_days_list)""" # 建立輸出數(shù)據(jù)的DataFrame """ # 序號(hào) s = len(Every_Name_company) Number_list = [i for i in range(1, s + 1)] print(s) # 公司 Company_Every_person_list_all = Company_Every_person_list # 姓名 Name_list = Every_Name_company # 部門 Department_list = department_Every_person_list# 生成DataFrame df_3 = {"序號(hào)": Number_list, "公司": Company_Every_person_list_all, "姓名": Name_list, "部門": Department_list,"0.工作日正常小時(shí)": work_time_total_list_all, "1.工作日加班小時(shí)": overtime_workday_total_list_all,"2.雙休日加班小時(shí)": weekend_overtime_workday_list_all,"3.法定節(jié)假日加班小時(shí)": holiday_overtime_workday_list_all, "工作日出勤天數(shù)": workday_numbers_list} df_3 = pd.DataFrame(df_3) print(df_3) df_3.to_excel("D:\\", index=False)總結(jié)
- 上一篇: 拯救2K屏手机!修改屏幕分辨率省电教程
- 下一篇: Docker 集群Swarm创建和Swa