Functional Weave
Code in Python

time.round-to-increment@1.0.0

impl/python.py

741 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 .math_round_div import round_div  ← from math.round-div ^1.0.0 · built alongside by fune
from .math_round_div_types import RoundingMode


def round_to_increment(minutes: int, increment: int, mode: RoundingMode) -> int:
    """Round a duration to a whole number of billing increments. The rounding is
    math.round-div's, so "half-up" here is the same rule as for money.
    """
    if isinstance(minutes, bool) or not isinstance(minutes, int) or minutes < 0:
        raise ValueError("minutes must be a non-negative integer, received %s" % (minutes,))
    if isinstance(increment, bool) or not isinstance(increment, int) or increment < 1:
        raise ValueError("increment must be a positive integer, received %s" % (increment,))
    return round_div(minutes, increment, mode) * increment