diff options
| -rw-r--r-- | bot/utils/time.py | 20 | 
1 files changed, 20 insertions, 0 deletions
diff --git a/bot/utils/time.py b/bot/utils/time.py index 9520b32f8..ac64865d6 100644 --- a/bot/utils/time.py +++ b/bot/utils/time.py @@ -138,3 +138,23 @@ def format_infraction_with_duration(      duration_formatted = f" ({duration})" if duration else ''      return f"{expiry_formatted}{duration_formatted}" + + +def until_expiration(expiry: Optional[str], max_units: int = 2) -> Optional[str]: +    """ +    Get the remaining time until infraction's expiration, in a human-readable version of the relativedelta. + +    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. +    """ +    if not expiry: +        return None + +    now = datetime.datetime.utcnow() +    since = dateutil.parser.isoparse(expiry).replace(tzinfo=None, microsecond=0) + +    if since < now: +        return None + +    return humanize_delta(relativedelta(since, now), max_units=max_units)  |