🛠️ToolsShed

Calculadora de dias úteis

Conta os dias úteis entre duas datas, excluindo fins de semana.

Perguntas Frequentes

Implementação de Código

from datetime import date, timedelta

def add_business_days(start: date, days: int, holidays: list[date] = None) -> date:
    """Add a given number of business days to a start date."""
    holidays = set(holidays or [])
    current = start
    remaining = days
    step = 1 if days >= 0 else -1

    while remaining != 0:
        current += timedelta(days=step)
        if current.weekday() < 5 and current not in holidays:  # Mon-Fri
            remaining -= step
    return current

def count_business_days(start: date, end: date, holidays: list[date] = None) -> int:
    """Count business days between two dates (exclusive of start)."""
    holidays = set(holidays or [])
    count = 0
    current = start
    while current < end:
        current += timedelta(days=1)
        if current.weekday() < 5 and current not in holidays:
            count += 1
    return count

# Example
start = date(2024, 1, 1)
result = add_business_days(start, 10)
print(f"10 business days after {start}: {result}")

biz_days = count_business_days(date(2024, 1, 1), date(2024, 1, 31))
print(f"Business days in January 2024: {biz_days}")

Comments & Feedback

Comments are powered by Giscus. Sign in with GitHub to leave a comment.