mirror of
https://github.com/complexcaresolutions/dak.c2s.git
synced 2026-03-17 21:53:41 +00:00
50 lines
1.3 KiB
Python
50 lines
1.3 KiB
Python
"""Calendar week and date utilities."""
|
|
|
|
from datetime import date
|
|
|
|
|
|
def date_to_kw(d: date) -> int:
|
|
"""Return ISO calendar week number."""
|
|
return d.isocalendar()[1]
|
|
|
|
|
|
def date_to_jahr(d: date) -> int:
|
|
"""Return ISO calendar year (can differ from d.year at year boundaries)."""
|
|
return d.isocalendar()[0]
|
|
|
|
|
|
def parse_german_date(s: str) -> date:
|
|
"""Parse German date formats: DD.MM.YY, DD.MM.YYYY, 'DD.MM.YY, HH:MM'.
|
|
|
|
Handles edge cases:
|
|
- Two-digit years: 00-49 -> 2000-2049, 50-99 -> 1950-1999
|
|
- Invalid dates like '29.08.0196' -> raises ValueError
|
|
- Leading/trailing whitespace
|
|
- Comma-separated datetime: '02.02.26, 08:50' -> takes date part only
|
|
"""
|
|
s = s.strip()
|
|
if not s:
|
|
raise ValueError("Empty date string")
|
|
|
|
# Split off time part if present (e.g., "02.02.26, 08:50")
|
|
if "," in s:
|
|
s = s.split(",")[0].strip()
|
|
|
|
parts = s.split(".")
|
|
if len(parts) != 3:
|
|
raise ValueError(f"Invalid date format: '{s}'")
|
|
|
|
day = int(parts[0])
|
|
month = int(parts[1])
|
|
year_str = parts[2].strip()
|
|
year = int(year_str)
|
|
|
|
# Handle 2-digit years
|
|
if year < 100:
|
|
year = 2000 + year if year < 50 else 1900 + year
|
|
|
|
# Reject obviously wrong years
|
|
if year < 1900 or year > 2100:
|
|
raise ValueError(f"Year out of range: {year}")
|
|
|
|
return date(year, month, day)
|