🛠️ToolsShed

Calcolatore Ore Lavorative

Calcola il totale delle ore lavorative al giorno e alla settimana con deduzioni per le pause.

#InizioFinePausa (min)Ore
108:00
208:00
308:00
408:00
508:00

Totale settimanale

40:00

Straordinari (>8h/giorno)

00:00

Domande Frequenti

Implementazione del Codice

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.