diff options
| author | 2022-08-01 00:55:00 -0400 | |
|---|---|---|
| committer | 2022-08-01 00:55:00 -0400 | |
| commit | b699d114c5fb5810915dc85f5aa0649a0b091a79 (patch) | |
| tree | 9c3b303b3e62d60fc863efde0f0af9439fc15382 | |
| parent | Added new expiry usage to apply (diff) | |
Added microsecond rounding for `humanize_delta`
Diffstat (limited to '')
| -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)  |