工时计算程序
工時計算
# coding=utf-8 import numpy as np import pandas as pd import datetime import collections# 計算時間差函數 def difftime(click_in, click_out):# 輸入的日期和時間是字符串形式,需要先將字符串格式化為datetime形式。time1 = datetime.datetime.strptime(click_in, "%H:%M")time2 = datetime.datetime.strptime(click_out, "%H:%M")num = (time2-time1).seconds/60return num# 得到每一個姓名下所有數據的行所引 def checkId(target, a):b = []for index, nums in enumerate(a):if nums == target:b.append(index)return (b)in_path = "20210501-20210531_1(無單多次打卡數據).xlsx" df = pd.read_excel(in_path, skiprows=0, header=0) print(df) # 提取工時計算所需數據 Name = df["姓名"].astype("str") Company = df["部門"].astype("str") Date_attendance = df["考勤日期"].astype("str") Clock_time = df["打卡時間"].astype("str") Group_attendance = df["考勤組"].astype("str") Clock_result = df["打卡結果"].astype("str")""" # 計算該公司內每個員工的,0.工作日正常小時、1.工作日加班小時、2.雙休日加班小時、3.法定節假日加班小時 """ Every_Name_company = [item for item, count in collections.Counter(Name).items() if count >= 1] # 遍歷每個員工,利用相應數據計算出四個工時 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)# 這個人考勤日期、打卡時間的索引Date_index_Every_person = checkId(i, Name)# 取出每個人的考勤日期、打卡時間數據Date_attendance_Every_person = Date_attendance[Date_index_Every_person]Clock_time_Every_person = Clock_time[Date_index_Every_person]# 數據劃分。三類數據,周一-周五:工作日,周六、周日:雙休日,法定節假日(待實現,周主任要公司年歷,調休規則),以考勤日期進行篩選。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月份調休規則: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# 節假日索引holiday_index_all = holiday_index_all + holiday_index# 剔除正常工作日、雙休日和節假日重復索引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)# 計算工時"""# 這個人工作日每天正常小時計算,周一至周五"""# 工作日正常工作work_time_std_1 = []work_time_std_2 = []work_time_early = []work_time_late = []late_early_time = []overtime_workday = []# 工作日工時Clock_time_Every_person_work = Clock_time[work_index_all]# 每次取出連續兩行數據,每次拿出每個人一天的打卡時間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])# 字符串切片,為了比較,只要時間字符串,不要日期字符clock_in_time = clock_in_time[11: 16]clock_out_time = clock_out_time[11: 16]# 工作日正常小時計算clock_in_std_time = "08:30"clock_out_std_time = "18:00"# 如果上班打卡早于8:30,下班打卡晚于18:00,這一天工作日正常小時為8小時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點,下班早于18點,如果下班早于18點,彈性工作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]# 遲到情況工時計算elif clock_in_time.__gt__(clock_in_std_time) is True and clock_out_time.__gt__(clock_out_std_time) is True:# 遲到時間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]# 計算工作日加班時間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# 計算這個人的工作日天數workday_numbers = workday_numbers + 1# 計算雙休日加班時間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])# 字符串切片,為了比較,只要時間字符串,不要日期字符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是中午吃飯時間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# 計算法定節假日加班時間,與周六日加班時間如出一轍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])# 字符串切片,為了比較,只要時間字符串,不要日期字符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是中午吃飯時間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# 匯 總 生成各種工時統計列表work_time_total_list = work_time_std_1 + work_time_std_2 + work_time_early + work_time_late + late_early_time# 每個人的工作日正常小時work_time_total = round(sum(work_time_total_list) / 60)work_time_total_list_all.append(work_time_total)print("工作日工時", work_time_total)# 每個人工作日加班小時overtime_workday_total = round(sum(overtime_workday) / 60)overtime_workday_total_list_all.append(overtime_workday_total)print("工作日加班小時", overtime_workday_total)# 每個人雙休日加班小時weekend_overtime_workday_list = round(sum(weekend_overtime_workday_list) / 60)weekend_overtime_workday_list_all.append(weekend_overtime_workday_list)print("雙休日加班小時", 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("法定節假日加班小時", holiday_overtime_workday_list)# 每個人對應公司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)# 天數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)""" # 建立輸出數據的DataFrame """ # 序號 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 = {"序號": Number_list, "公司": Company_Every_person_list_all, "姓名": Name_list, "部門": Department_list,"0.工作日正常小時": work_time_total_list_all, "1.工作日加班小時": overtime_workday_total_list_all,"2.雙休日加班小時": weekend_overtime_workday_list_all,"3.法定節假日加班小時": holiday_overtime_workday_list_all, "工作日出勤天數": workday_numbers_list} df_3 = pd.DataFrame(df_3) print(df_3) df_3.to_excel("D:\\", index=False)總結
- 上一篇: 拯救2K屏手机!修改屏幕分辨率省电教程
- 下一篇: Docker 集群Swarm创建和Swa