from .dates_add_days import civil_from_days, days_from_civil, epoch_day_from_iso from .dates_day_of_week import day_of_week from .dates_week_number_types import IsoWeek def week_number(iso: str) -> IsoWeek: """The ISO 8601 week of a date. The Thursday of the date's Monday-to-Sunday week decides everything: its calendar year is the week-year, and its day of that year gives the week number. """ thursday = epoch_day_from_iso(iso) + (4 - day_of_week(iso)) week_year = civil_from_days(thursday).year week = (thursday - days_from_civil(week_year, 1, 1)) // 7 + 1 return IsoWeek(week_year=week_year, week=week, label="%04d-W%02d" % (week_year, week))