Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
worktime solution in Clear category for Working Hours Calculator by imloafer
from datetime import datetime, timedelta
def working_hours(
date1: str, date2: str, start_time: str, end_time: str, holy: list[str]
) -> int:
date1, date2 = datetime.strptime(date1, '%Y-%m-%d'), datetime.strptime(date2, '%Y-%m-%d')
start_time, end_time = datetime.strptime(start_time, '%H:%M'), datetime.strptime(end_time, '%H:%M')
hours = (end_time - start_time).total_seconds()/3600
days = (date2 - date1).days + 1
totaldays = sum((date1 + timedelta(days=day)).isoweekday() not in [6, 7] for day in range(days))
holidays = sum(date1 <= datetime.strptime(day, '%Y-%m-%d') <= date2 for day in holy)
return (totaldays - holidays) * hours
print("Example:")
print(working_hours("2023-03-01", "2023-03-31", "09:00", "17:00", []))
# These "asserts" are used for self-checking
assert working_hours("2023-03-01", "2023-03-01", "09:00", "17:00", []) == 8
assert working_hours("2023-03-01", "2023-03-02", "09:00", "17:00", []) == 16
assert working_hours("2023-03-01", "2023-03-03", "09:00", "17:00", ["2023-03-01"]) == 16
print("The mission is done! Click 'Check Solution' to earn rewards!")
March 3, 2023