🛠️ToolsShed

Time Card Calculator

Track weekly work hours with break deductions and overtime calculation.

DayStartEndHours
7.50h
7.50h
7.50h
7.50h
7.50h
Weekly Total
37.50h
Avg Daily
7.50h
Regular Hours
37.50h
Overtime (>40h)
0.00h

Frequently Asked Questions

Code Implementation

from datetime import datetime, timedelta

def calculate_time_card(entries: list[dict]) -> dict:
    """Calculate total hours from clock-in/clock-out pairs.
    Each entry: {"clock_in": "HH:MM", "clock_out": "HH:MM", "break_minutes": 0}
    """
    total_seconds = 0
    for entry in entries:
        fmt = "%H:%M"
        clock_in = datetime.strptime(entry["clock_in"], fmt)
        clock_out = datetime.strptime(entry["clock_out"], fmt)
        if clock_out < clock_in:
            clock_out += timedelta(days=1)  # overnight shift
        duration = clock_out - clock_in
        break_secs = entry.get("break_minutes", 0) * 60
        total_seconds += max(0, duration.total_seconds() - break_secs)

    hours = int(total_seconds // 3600)
    minutes = int((total_seconds % 3600) // 60)
    return {
        "total_seconds": total_seconds,
        "total_hours": total_seconds / 3600,
        "formatted": f"{hours}h {minutes:02d}m",
    }

# Example time card
entries = [
    {"clock_in": "09:00", "clock_out": "12:30", "break_minutes": 0},
    {"clock_in": "13:00", "clock_out": "17:30", "break_minutes": 15},
    {"clock_in": "22:00", "clock_out": "06:00", "break_minutes": 30},  # overnight
]
result = calculate_time_card(entries)
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.