diff options
author | 2019-12-04 15:03:16 +0700 | |
---|---|---|
committer | 2019-12-04 15:03:16 +0700 | |
commit | 51f80015c5db9ab8e85ea2304789491d4c72c053 (patch) | |
tree | 12bd814cc13fbd0b52389d2fd4785ebe9b35c4e9 | |
parent | Improved type hinting for `format_infraction_with_duration` (diff) |
Created `until_expiration` to get the remaining time until the infraction expires.
-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) |