aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Shirayuki Nekomata <[email protected]>2019-11-27 14:34:36 +0700
committerGravatar Shirayuki Nekomata <[email protected]>2019-11-27 14:34:36 +0700
commitdadb91573c519c1444608ce0cce3de7b01b860a9 (patch)
treed580a8f8623db5d38e0b84fb5a7966fab6aaf031
parentAdded pytest for `get_duration()` (diff)
Implemented `get_duration_from_expiry()` which call `get_duration()` for `expiry` and `datetime.utcnow()`
-rw-r--r--bot/utils/time.py18
1 files changed, 18 insertions, 0 deletions
diff --git a/bot/utils/time.py b/bot/utils/time.py
index 740ede0d3..00f39b940 100644
--- a/bot/utils/time.py
+++ b/bot/utils/time.py
@@ -145,3 +145,21 @@ def get_duration(date_from: datetime.datetime, date_to: datetime.datetime) -> st
results.append(f"{amount:.0f} {name}{plural}")
# We have to reverse the order of units because currently it's smallest -> largest
return ', '.join(results[::-1][:2])
+
+
+def get_duration_from_expiry(expiry: str) -> str:
+ """
+ Get the duration between datetime.utcnow() and an expiry, in human readable format.
+
+ Will return the two biggest units avaiable, for example:
+ - 11 hours, 59 minutes
+ - 1 week, 6 minutes
+ - 7 months, 2 weeks
+ - 3 years, 3 months
+ - 5 minutes
+
+ :param expiry: A string.
+ """
+ date_from = datetime.datetime.utcnow()
+ date_to = dateutil.parser.isoparse(expiry)
+ return get_duration(date_from, date_to)