diff options
author | 2021-08-06 15:25:17 -0700 | |
---|---|---|
committer | 2021-08-06 15:25:17 -0700 | |
commit | 07b345eed59e775977da202602ed1c9568cca494 (patch) | |
tree | 539b2ffcc08c76ea4bb0434f7d72439cb529424c | |
parent | Time: add overload to pass 2 timestamps to humanize_delta (diff) |
Time: add overload to pass relativedelta kwargs to humanize_delta
-rw-r--r-- | bot/exts/moderation/slowmode.py | 3 | ||||
-rw-r--r-- | bot/utils/time.py | 33 |
2 files changed, 32 insertions, 4 deletions
diff --git a/bot/exts/moderation/slowmode.py b/bot/exts/moderation/slowmode.py index da04d1e98..b6a771441 100644 --- a/bot/exts/moderation/slowmode.py +++ b/bot/exts/moderation/slowmode.py @@ -39,8 +39,7 @@ class Slowmode(Cog): if channel is None: channel = ctx.channel - delay = relativedelta(seconds=channel.slowmode_delay) - humanized_delay = time.humanize_delta(delay) + humanized_delay = time.humanize_delta(seconds=channel.slowmode_delay) await ctx.send(f'The slowmode delay for {channel.mention} is {humanized_delay}.') diff --git a/bot/utils/time.py b/bot/utils/time.py index 7e314a870..6fc43ef6a 100644 --- a/bot/utils/time.py +++ b/bot/utils/time.py @@ -103,11 +103,29 @@ def humanize_delta( ... +@overload +def humanize_delta( + *, + years: int = 0, + months: int = 0, + weeks: float = 0, + days: float = 0, + hours: float = 0, + minutes: float = 0, + seconds: float = 0, + precision: str = "seconds", + max_units: int = 6, + absolute: bool = True, +) -> str: + ... + + def humanize_delta( *args, precision: str = "seconds", max_units: int = 6, absolute: bool = True, + **kwargs, ) -> str: """ Return a human-readable version of a time duration. @@ -121,6 +139,12 @@ def humanize_delta( Usage: + Keyword arguments specifying values for time units, to construct a `relativedelta` and humanize + the duration represented by it: + + >>> humanize_delta(days=2, hours=16, seconds=23) + '2 days, 16 hours and 23 seconds' + **One** `relativedelta` object, to humanize the duration represented by it: >>> humanize_delta(relativedelta(years=12, months=6)) @@ -157,9 +181,14 @@ def humanize_delta( Instead, it's relative to the `datetime` to which it's added to get the other `datetime`. In the example, the difference arises because all months don't have the same number of days. """ - if len(args) == 1 and isinstance(args[0], relativedelta): + if args and kwargs: + raise ValueError("Unsupported combination of positional and keyword arguments.") + + if len(args) == 0: + delta = relativedelta(**kwargs) + elif len(args) == 1 and isinstance(args[0], relativedelta): delta = args[0] - elif 1 <= len(args) <= 2: + elif len(args) <= 2: end = arrow.get(args[0]) start = arrow.get(args[1]) if len(args) == 2 else arrow.utcnow() |