diff options
Diffstat (limited to '')
| -rw-r--r-- | bot/utils/time.py | 36 | 
1 files changed, 23 insertions, 13 deletions
| diff --git a/bot/utils/time.py b/bot/utils/time.py index 7416f36e0..77060143c 100644 --- a/bot/utils/time.py +++ b/bot/utils/time.py @@ -114,30 +114,40 @@ def format_infraction(timestamp: str) -> str:  def format_infraction_with_duration( -    expiry: Optional[str], +    date_to: Optional[str],      date_from: Optional[datetime.datetime] = None, -    max_units: int = 2 +    max_units: int = 2, +    absolute: bool = True  ) -> Optional[str]:      """ -    Format an infraction timestamp to a more readable ISO 8601 format WITH the duration. +    Return `date_to` formatted as a readable ISO-8601 with the humanized duration since `date_from`. -    Returns a human-readable version of the duration between datetime.utcnow() and an expiry. -    Unlike `humanize_delta`, this function will force the `precision` to be `seconds` by not passing it. -    `max_units` specifies the maximum number of units of time to include (e.g. 1 may include days but not hours). -    By default, max_units is 2. +    `date_from` must be an ISO-8601 formatted timestamp. The duration is calculated as from +    `date_from` until `date_to` with a precision of seconds. If `date_from` is unspecified, the +    current time is used. + +    `max_units` specifies the maximum number of units of time to include in the duration. For +    example, a value of 1 may include days but not hours. + +    If `absolute` is True, the absolute value of the duration delta is used. This prevents negative +    values in the case that `date_to` is in the past relative to `date_from`.      """ -    if not expiry: +    if not date_to:          return None +    date_to_formatted = format_infraction(date_to) +      date_from = date_from or datetime.datetime.utcnow() -    date_to = dateutil.parser.isoparse(expiry).replace(tzinfo=None, microsecond=0) +    date_to = dateutil.parser.isoparse(date_to).replace(tzinfo=None, microsecond=0) -    expiry_formatted = format_infraction(expiry) +    delta = relativedelta(date_to, date_from) +    if absolute: +        delta = abs(delta) -    duration = humanize_delta(relativedelta(date_to, date_from), max_units=max_units) -    duration_formatted = f" ({duration})" if duration else '' +    duration = humanize_delta(delta, max_units=max_units) +    duration_formatted = f" ({duration})" if duration else "" -    return f"{expiry_formatted}{duration_formatted}" +    return f"{date_to_formatted}{duration_formatted}"  def until_expiration( | 
