diff options
author | 2022-08-01 00:55:00 -0400 | |
---|---|---|
committer | 2022-08-16 16:38:26 -0400 | |
commit | 3babbf2fa0a6a8d99e796af0cd78a7516b781898 (patch) | |
tree | dafe317fa0bc529fff4e92e62e8ffbd280afe88c | |
parent | Added new expiry usage to apply (diff) |
Added microsecond rounding for `humanize_delta`
-rw-r--r-- | bot/utils/time.py | 17 |
1 files changed, 16 insertions, 1 deletions
diff --git a/bot/utils/time.py b/bot/utils/time.py index a0379c3ef..104ea026d 100644 --- a/bot/utils/time.py +++ b/bot/utils/time.py @@ -195,7 +195,11 @@ def humanize_delta( end = arrow.get(args[0]) start = arrow.get(args[1]) if len(args) == 2 else arrow.utcnow() - delta = relativedelta(end.datetime, start.datetime) + # Round microseconds + end = round_datetime(end.datetime) + start = round_datetime(start.datetime) + + delta = relativedelta(end, start) if absolute: delta = abs(delta) else: @@ -326,3 +330,14 @@ def until_expiration(expiry: Optional[Timestamp]) -> str: return "Expired" return format_relative(expiry) + + +def round_datetime(dt: datetime.datetime) -> datetime.datetime: + """ + Round a datetime object to the nearest second. + + Resulting datetime objects will have microsecond values of 0, useful for delta comparisons. + """ + if dt.microsecond >= 500000: + dt += datetime.timedelta(seconds=1) + return dt.replace(microsecond=0) |