47 lines
1.5 KiB
Python
47 lines
1.5 KiB
Python
#Kalender.py
|
|
import calendar
|
|
from datetime import date
|
|
|
|
month_name = ["Januar", "Februar", "Maerz", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember"]
|
|
|
|
# Definition Schaltjahr
|
|
def leap_year(year):
|
|
return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
|
|
|
|
# Definition Anzahl Tage pro Monat
|
|
def days_leap_day(year, month):
|
|
days_per_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
|
|
days = days_per_month[month - 1]
|
|
if month == 2 and leap_year(year):
|
|
days += 1
|
|
return days
|
|
|
|
# Calculate the calendar week number (ISO 8601)
|
|
def calendar_week(year, month, day):
|
|
d = date(year, month, day)
|
|
return d.isocalendar()[1]
|
|
|
|
# Definition Ausgabe
|
|
def calender(year):
|
|
for m in range(1, 13):
|
|
print(f"{month_name[m-1]} {year}".center(28))
|
|
print("KW MO DI MI DO FR SA SO")
|
|
|
|
month_calendar = calendar.monthcalendar(year, m)
|
|
for week in month_calendar:
|
|
# Check if the week contains days from the current month
|
|
if all(day == 0 for day in week):
|
|
continue
|
|
|
|
# Get the correct week number
|
|
week_num = calendar_week(year, m, next(day for day in week if day != 0))
|
|
print(f"{week_num:2}", end=" ")
|
|
for day in week:
|
|
if day == 0:
|
|
print(" ", end="")
|
|
else:
|
|
print(f"{day:2} ", end="")
|
|
print()
|
|
print()
|
|
|
|
calender(2024) |