Functional Weave
Code in Python

subscriptions.trial-end@1.0.0

impl/python.py

1,051 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 add_days  ← from dates.add-days ^1.0.0 · built alongside by fune
from .subscriptions_trial_end_types import TrialPeriod


def trial_end(start_date: str, trial_days: int) -> TrialPeriod:
    """The last free day and the first charge date of a trial of ``trial_days``
    days starting on ``start_date``. The start day counts as day one, so the
    last free day is start + trial_days - 1 and the charge is the day after it.
    """
    if isinstance(trial_days, bool) or not isinstance(trial_days, int):
        raise TypeError("trial days must be an integer, received %r" % (trial_days,))
    if trial_days < 0:
        raise ValueError("trial days must not be negative, received %d" % (trial_days,))
    # add_days validates the start date even when there is no trial, so a
    # malformed date never slips through as the "first charge date".
    first_charge_date = add_days(start_date, trial_days)
    last_trial_day = None if trial_days == 0 else add_days(start_date, trial_days - 1)
    return TrialPeriod(last_trial_day=last_trial_day, first_charge_date=first_charge_date)