| 1 |
nino.borges |
889 |
"""
|
| 2 |
|
|
|
| 3 |
|
|
TimeEntryTools
|
| 4 |
|
|
|
| 5 |
|
|
Created by:
|
| 6 |
|
|
Emanuel Borges
|
| 7 |
|
|
12.09.2024
|
| 8 |
|
|
|
| 9 |
|
|
A program that will help me QC some of my time entry.
|
| 10 |
|
|
|
| 11 |
|
|
"""
|
| 12 |
|
|
|
| 13 |
|
|
from datetime import datetime
|
| 14 |
|
|
|
| 15 |
|
|
|
| 16 |
|
|
def calculate_time_worked(start_time_str, lunch_break, end_time_str, additional_back_to_work_str = None, additional_end_work_str = None):
|
| 17 |
|
|
try:
|
| 18 |
|
|
## Convert main work period to datetime objects
|
| 19 |
|
|
start_time = datetime.strptime(start_time_str, "%I:%M%p")
|
| 20 |
|
|
end_time = datetime.strptime(end_time_str, "%I:%M%p")
|
| 21 |
|
|
|
| 22 |
|
|
|
| 23 |
|
|
## Calculate total work hours excluding lunch break
|
| 24 |
|
|
total_hours = (end_time - start_time).total_seconds() / 3600
|
| 25 |
|
|
total_hours -= lunch_break
|
| 26 |
|
|
|
| 27 |
|
|
|
| 28 |
|
|
## Handle additional work period if provided
|
| 29 |
|
|
if additional_back_to_work_str and additional_end_work_str:
|
| 30 |
|
|
additional_start = datetime.strptime(additional_back_to_work_str, "%I:%M%p")
|
| 31 |
|
|
additional_end = datetime.strptime(additional_end_work_str, "%I:%M%p")
|
| 32 |
|
|
additional_hours = (additional_end - additional_start).total_seconds() /3600
|
| 33 |
|
|
total_hours += additional_hours
|
| 34 |
|
|
|
| 35 |
|
|
|
| 36 |
|
|
## Ensure no negative time if returned
|
| 37 |
|
|
return max(total_hours,0)
|
| 38 |
|
|
except Exception as e:
|
| 39 |
|
|
print(f"An error occurred: {e}")
|
| 40 |
|
|
return None
|
| 41 |
|
|
|
| 42 |
|
|
|
| 43 |
|
|
|
| 44 |
|
|
|
| 45 |
|
|
|
| 46 |
|
|
if __name__ == '__main__':
|
| 47 |
|
|
additional_back_to_work = None
|
| 48 |
|
|
additional_end_work = None
|
| 49 |
|
|
|
| 50 |
|
|
start_time = "09:00AM"
|
| 51 |
|
|
lunch_break = 1 #45 munutes is .75
|
| 52 |
|
|
end_time = "6:30PM"
|
| 53 |
|
|
additional_back_to_work = "08:30PM"
|
| 54 |
|
|
additional_end_work = "10:00PM"
|
| 55 |
|
|
|
| 56 |
|
|
|
| 57 |
|
|
|
| 58 |
|
|
## Calculate total hours worked
|
| 59 |
|
|
hours_worked = calculate_time_worked(start_time, lunch_break, end_time, additional_back_to_work, additional_end_work)
|
| 60 |
|
|
print(hours_worked)
|
| 61 |
|
|
print(f"Total hours worked: {hours_worked:.2f} hours.") |