有开工打卡,挂起打卡,重新开工,完工打卡,怎么计算工时代码
时间: 2023-10-14 08:06:04 浏览: 104
以下是一个简单的示例代码,假设我们有一个工作日历的类`WorkCalendar`,其中包含了工作日历的信息(工作日、休息日、假期等),以及一个打卡记录的类`WorkRecord`,其中包含了每个任务的打卡记录。
```
class WorkCalendar:
def __init__(self, start_date, end_date):
# 初始化工作日历
pass
def is_workday(self, date):
# 判断某一天是否是工作日
pass
class WorkRecord:
def __init__(self, task_name):
self.task_name = task_name
self.records = []
def start_work(self, timestamp):
self.records.append(('start', timestamp))
def suspend_work(self, timestamp):
self.records.append(('suspend', timestamp))
def resume_work(self, timestamp):
self.records.append(('resume', timestamp))
def finish_work(self, timestamp):
self.records.append(('finish', timestamp))
def get_work_time(self):
work_time = 0
start_time = None
suspend_time = None
resume_time = None
for record in self.records:
action, timestamp = record
if action == 'start':
start_time = timestamp
elif action == 'suspend':
suspend_time = timestamp
elif action == 'resume':
resume_time = timestamp
work_time += resume_time - suspend_time
elif action == 'finish':
if resume_time:
work_time += timestamp - resume_time
else:
work_time += timestamp - start_time
return work_time
class WorkHourCalculator:
def __init__(self, work_calendar):
self.work_calendar = work_calendar
def calculate_work_hour(self, work_records):
total_work_time = 0
for work_record in work_records:
work_time = work_record.get_work_time()
if work_time > 0:
total_work_time += work_time
return total_work_time
```
使用以上代码,我们可以先初始化一个工作日历对象,然后创建多个打卡记录对象,记录每个任务的打卡记录。最后将所有打卡记录传入工时计算器,即可得到总的工作时间。
以下是一个简单的示例:
```
# 初始化工作日历
work_calendar = WorkCalendar(start_date='2022-01-01', end_date='2022-12-31')
# 创建打卡记录对象
work_record1 = WorkRecord(task_name='task1')
work_record1.start_work('2022-01-01 09:00:00')
work_record1.suspend_work('2022-01-01 12:00:00')
work_record1.resume_work('2022-01-01 13:00:00')
work_record1.finish_work('2022-01-01 18:00:00')
work_record2 = WorkRecord(task_name='task2')
work_record2.start_work('2022-01-02 09:00:00')
work_record2.suspend_work('2022-01-02 12:00:00')
work_record2.resume_work('2022-01-03 09:00:00')
work_record2.finish_work('2022-01-03 18:00:00')
# 计算总的工作时间
work_hour_calculator = WorkHourCalculator(work_calendar)
work_time = work_hour_calculator.calculate_work_hour([work_record1, work_record2])
print(f'Total work time: {work_time} seconds')
```
阅读全文