Functional Weave
Code in Python

dates.day-of-week@1.0.0

impl/python.py

1,264 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 epoch_day_from_iso  ← from dates.add-days ^1.0.0 · built alongside by fune

DAY_NAMES = (
    "Monday",
    "Tuesday",
    "Wednesday",
    "Thursday",
    "Friday",
    "Saturday",
    "Sunday",
)


def day_of_week(iso: str) -> int:
    """The ISO 8601 day of the week: 1 = Monday through 7 = Sunday.

    ISO numbering rather than the 0 = Monday that Python's own weekday() uses
    or the 0 = Sunday that JavaScript uses, because ISO is what the rest of the
    business world means by "day 1" and because a weekend test becomes
    ``day >= 6`` in every language.
    """
    # 1970-01-01, epoch day 0, was a Thursday. Shifting by 3 moves the origin of
    # the cycle to Monday so that the remainder is already ISO numbering.
    return (epoch_day_from_iso(iso) + 3) % 7 + 1


def is_weekend(iso: str) -> bool:
    """True for Saturday and Sunday."""
    return day_of_week(iso) >= 6


def is_weekday(iso: str) -> bool:
    """True for Monday to Friday. Says nothing about public holidays."""
    return day_of_week(iso) <= 5


def day_name(iso: str) -> str:
    """The English name, for logs and labels.

    Not localised, deliberately: a capability that needed locale data would need
    a locale argument and a data file to pin it.
    """
    return DAY_NAMES[day_of_week(iso) - 1]