Functional Weave
Code in Rust

charts.format@1.0.0

impl/python/format_date.py

757 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 .charts_format_types import DateLabel
from .dates_add_days import parse_iso_date  ← from dates.add-days ^1.0.0 · built alongside by fune

_MONTHS = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]


def format_date(iso: str, label: DateLabel) -> str:
    """A date as a tick label for the interval it stands for."""
    date = parse_iso_date(iso)
    year = iso[0:4]
    month = _MONTHS[date.month - 1]
    if label in ("day", "week"):
        return "%d %s" % (date.day, month)
    if label == "month":
        return "%s %s" % (month, year)
    if label == "quarter":
        return "Q%d %s" % ((date.month - 1) // 3 + 1, year)
    if label == "year":
        return year
    raise ValueError('unknown date label "%s"; use day, week, month, quarter or year' % (label,))