Functional Weave
Code in Rust

dates.week-number@1.0.0

impl/python.py

687 bytes · the Python implementation · view raw

Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.

from .dates_add_days import civil_from_days, days_from_civil, epoch_day_from_iso  ← from dates.add-days ^1.0.0 · built alongside by fune
from .dates_day_of_week import day_of_week  ← from dates.day-of-week ^1.0.0 · built alongside by fune
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))