🛠️ToolsShed

Progresso dell'Anno

Guarda quanto dell'anno corrente è trascorso e quanto resta.

Domande Frequenti

Implementazione del Codice

from datetime import datetime, date

def year_progress(now: datetime = None) -> dict:
    """Calculate the percentage of the current year elapsed."""
    if now is None:
        now = datetime.now()

    year = now.year
    year_start = datetime(year, 1, 1)
    year_end = datetime(year + 1, 1, 1)

    elapsed = (now - year_start).total_seconds()
    total = (year_end - year_start).total_seconds()

    percent = (elapsed / total) * 100
    days_in_year = 366 if (year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)) else 365
    day_of_year = now.timetuple().tm_yday
    days_remaining = days_in_year - day_of_year

    return {
        "year": year,
        "percent": round(percent, 4),
        "day_of_year": day_of_year,
        "days_in_year": days_in_year,
        "days_remaining": days_remaining,
        "is_leap_year": days_in_year == 366,
    }

# Example
info = year_progress()
print(f"Year {info['year']} is {info['percent']:.2f}% complete")
print(f"Day {info['day_of_year']} of {info['days_in_year']}")
print(f"{info['days_remaining']} days remaining")
print(f"Leap year: {info['is_leap_year']}")

Comments & Feedback

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