Calculateur d'Heures de Travail
Calculez le total des heures de travail par jour et par semaine avec déduction des pauses.
| # | Début | Fin | Pause (min) | Heures | |
|---|---|---|---|---|---|
| 1 | 08:00 | ||||
| 2 | 08:00 | ||||
| 3 | 08:00 | ||||
| 4 | 08:00 | ||||
| 5 | 08:00 |
Total hebdomadaire
40:00
Heures supplémentaires (>8h/jour)
00:00
Questions Fréquentes
Implémentation du Code
from datetime import datetime, timedelta
def working_hours(start: datetime, end: datetime, break_minutes: int = 0) -> dict:
"""Calculate working hours between two datetimes, subtracting break time."""
if end < start:
raise ValueError("End time must be after start time")
total_seconds = (end - start).total_seconds()
worked_seconds = max(0, total_seconds - break_minutes * 60)
hours = int(worked_seconds // 3600)
minutes = int((worked_seconds % 3600) // 60)
return {
"total_hours": worked_seconds / 3600,
"formatted": f"{hours}h {minutes:02d}m",
"break_hours": break_minutes / 60,
"gross_hours": total_seconds / 3600,
}
def weekly_hours(shifts: list[dict]) -> dict:
"""Calculate total working hours across multiple shifts."""
total_seconds = 0
for shift in shifts:
start = datetime.strptime(shift["start"], "%Y-%m-%d %H:%M")
end = datetime.strptime(shift["end"], "%Y-%m-%d %H:%M")
result = working_hours(start, end, shift.get("break_minutes", 0))
total_seconds += result["total_hours"] * 3600
h = int(total_seconds // 3600)
m = int((total_seconds % 3600) // 60)
return {"total_hours": total_seconds / 3600, "formatted": f"{h}h {m:02d}m"}
# Example
shifts = [
{"start": "2024-01-15 09:00", "end": "2024-01-15 17:30", "break_minutes": 60},
{"start": "2024-01-16 08:30", "end": "2024-01-16 16:00", "break_minutes": 30},
]
result = weekly_hours(shifts)
print(f"Total: {result['formatted']} ({result['total_hours']:.2f} hours)")Comments & Feedback
Comments are powered by Giscus. Sign in with GitHub to leave a comment.