aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Shirayuki Nekomata <[email protected]>2019-12-13 09:17:21 +0700
committerGravatar Shirayuki Nekomata <[email protected]>2019-12-13 09:17:21 +0700
commitc5109844e45c37bc1cc38eb1c3da31d52ab2aa6d (patch)
treecdac9e91ca6809594801b74e4c557294cbe37d19
parentMerge branch 'master' into Write-unit-tests-for-`bot/utils/time.py` (diff)
Adding an optional argument for `until_expiration`, update typehints for `format_infraction_with_duration`
- `until_expiration` was being a pain to unittests without a `now` ( default to `datetime.utcnow()` ). Adding an optional argument for this will not only make writing tests easier, but also allow more control over the helper function should we need to calculate the remaining time between two dates in the past. - Changed typehint for `date_from` in `format_infraction_with_duration` to `Optional[datetime.datetime]` to better reflect what it is.
-rw-r--r--bot/utils/time.py11
1 files changed, 8 insertions, 3 deletions
diff --git a/bot/utils/time.py b/bot/utils/time.py
index ac64865d6..7416f36e0 100644
--- a/bot/utils/time.py
+++ b/bot/utils/time.py
@@ -115,7 +115,7 @@ def format_infraction(timestamp: str) -> str:
def format_infraction_with_duration(
expiry: Optional[str],
- date_from: datetime.datetime = None,
+ date_from: Optional[datetime.datetime] = None,
max_units: int = 2
) -> Optional[str]:
"""
@@ -140,10 +140,15 @@ def format_infraction_with_duration(
return f"{expiry_formatted}{duration_formatted}"
-def until_expiration(expiry: Optional[str], max_units: int = 2) -> Optional[str]:
+def until_expiration(
+ expiry: Optional[str],
+ now: Optional[datetime.datetime] = None,
+ max_units: int = 2
+) -> Optional[str]:
"""
Get the remaining time until infraction's expiration, in a human-readable version of the relativedelta.
+ Returns a human-readable version of the remaining duration between datetime.utcnow() and an expiry.
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.
@@ -151,7 +156,7 @@ def until_expiration(expiry: Optional[str], max_units: int = 2) -> Optional[str]
if not expiry:
return None
- now = datetime.datetime.utcnow()
+ now = now or datetime.datetime.utcnow()
since = dateutil.parser.isoparse(expiry).replace(tzinfo=None, microsecond=0)
if since < now: