aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Johannes Christ <[email protected]>2018-07-22 10:00:58 +0000
committerGravatar Johannes Christ <[email protected]>2018-07-22 10:00:58 +0000
commitb626cafdfac2ea5fc622cfda72dce6523db51ef6 (patch)
tree039897a962b4ca5981d8d0d045469886ed60cf4a
parentMerge branch 'enhancement/paginate-and-sort-inventory-list' into 'master' (diff)
parentA utility for creating human readable timestrings. (diff)
Merge branch 'humanize_date' into 'master'
A utility for creating human readable timestrings. See merge request python-discord/projects/bot!33
-rw-r--r--bot/cogs/modlog.py23
-rw-r--r--bot/utils/time.py59
2 files changed, 61 insertions, 21 deletions
diff --git a/bot/cogs/modlog.py b/bot/cogs/modlog.py
index 8c449635c..67993dcd5 100644
--- a/bot/cogs/modlog.py
+++ b/bot/cogs/modlog.py
@@ -15,6 +15,7 @@ from discord.ext.commands import Bot
from bot.constants import Channels, Emojis, Icons
from bot.constants import Guild as GuildConstant
+from bot.utils.time import humanize
log = logging.getLogger(__name__)
@@ -305,27 +306,7 @@ class ModLog:
now = datetime.datetime.utcnow()
difference = abs(relativedelta(now, member.created_at))
- values = []
-
- if difference.years:
- values.append(f"{difference.years} years")
-
- if difference.months:
- values.append(f"{difference.months} months")
-
- if difference.days:
- values.append(f"{difference.days} days")
-
- if difference.hours:
- values.append(f"{difference.hours} hours")
-
- if difference.minutes:
- values.append(f"{difference.minutes} minutes")
-
- if difference.seconds:
- values.append(f"{difference.seconds} seconds")
-
- message += "\n\n**Account age:** " + ", ".join(values)
+ message += "\n\n**Account age:** " + humanize(difference)
if difference.days < 1 and difference.months < 1 and difference.years < 1: # New user account!
message = f"{Emojis.new} {message}"
diff --git a/bot/utils/time.py b/bot/utils/time.py
new file mode 100644
index 000000000..ef49c500f
--- /dev/null
+++ b/bot/utils/time.py
@@ -0,0 +1,59 @@
+from dateutil.relativedelta import relativedelta
+
+
+def _plural_timestring(value: int, unit: str) -> str:
+ """
+ Takes a value and a unit type,
+ such as 24 and "hours".
+
+ Returns a string that takes
+ the correct plural into account.
+
+ >>> _plural_timestring(1, "seconds")
+ "1 second"
+ >>> _plural_timestring(24, "hours")
+ "24 hours"
+ """
+
+ if value == 1:
+ return f"{value} {unit[:-1]}"
+ else:
+ return f"{value} {unit}"
+
+
+def humanize(delta: relativedelta, accuracy: str = "seconds") -> str:
+ """
+ This takes a relativedelta and
+ returns a nice human readable string.
+
+ "4 days, 12 hours and 1 second"
+
+ :param delta: A dateutils.relativedelta.relativedelta object
+ :param accuracy: The smallest unit that should be included.
+ :return: A humanized string.
+ """
+
+ units = {
+ "years": delta.years,
+ "months": delta.months,
+ "days": delta.days,
+ "hours": delta.hours,
+ "minutes": delta.minutes,
+ "seconds": delta.seconds
+ }
+
+ # Add the time units that are >0, but stop at accuracy.
+ time_strings = []
+ for unit, value in units.items():
+ if unit:
+ time_strings.append(_plural_timestring(value, unit))
+
+ if unit == accuracy:
+ break
+
+ # Add the 'and' between the last two units
+ if len(time_strings) > 1:
+ time_strings[-1] = f"{time_strings[-2]} and {time_strings[-1]}"
+ del time_strings[-2]
+
+ return ", ".join(time_strings)