From 345e8baab62b6fe98962b67eae224381857598a3 Mon Sep 17 00:00:00 2001 From: Ske Date: Sun, 16 Sep 2018 19:20:08 +0200 Subject: [PATCH] Minor nitpicks in the fronthistory command --- src/pluralkit/bot/commands/system_commands.py | 26 ++++++++++++------- src/pluralkit/utils.py | 3 ++- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/src/pluralkit/bot/commands/system_commands.py b/src/pluralkit/bot/commands/system_commands.py index 3cdd8438..2dfbb04f 100644 --- a/src/pluralkit/bot/commands/system_commands.py +++ b/src/pluralkit/bot/commands/system_commands.py @@ -1,6 +1,6 @@ import dateparser import humanize -from datetime import datetime +from datetime import datetime, timedelta import pluralkit.utils from pluralkit.bot import help @@ -172,14 +172,20 @@ async def system_frontpercent(ctx: CommandContext): system = await ctx.ensure_system() # Parse the time limit (will go this far back) - before = dateparser.parse(ctx.remaining(), languages=["en"], settings={ - "TO_TIMEZONE": "UTC", - "RETURN_AS_TIMEZONE_AWARE": False - }) + if ctx.remaining(): + before = dateparser.parse(ctx.remaining(), languages=["en"], settings={ + "TO_TIMEZONE": "UTC", + "RETURN_AS_TIMEZONE_AWARE": False + }) - # If time is in the future, just kinda discard - if before and before > datetime.utcnow(): - before = None + if not before: + return CommandError("Could not parse '{}' as a valid time.".format(ctx.remaining())) + + # If time is in the future, just kinda discard + if before and before > datetime.utcnow(): + before = None + else: + before = datetime.utcnow() - timedelta(days=30) # Fetch list of switches all_switches = await pluralkit.utils.get_front_history(ctx.conn, system.id, 99999) @@ -239,10 +245,10 @@ async def system_frontpercent(ctx: CommandContext): # Calculate percent fraction = front_time / total_time - percent = int(fraction * 100) + percent = round(fraction * 100) embed.add_field(name=member.name if member else "(no fronter)", value="{}% ({})".format(percent, humanize.naturaldelta(front_time))) - embed.set_footer(text="Since {}".format(span_start.isoformat(sep=" ", timespec="seconds"))) + embed.set_footer(text="Since {} ({})".format(span_start.isoformat(sep=" ", timespec="seconds"), humanize.naturaltime(pluralkit.utils.fix_time(span_start)))) await ctx.reply(embed=embed) diff --git a/src/pluralkit/utils.py b/src/pluralkit/utils.py index 04dbce4b..0763f962 100644 --- a/src/pluralkit/utils.py +++ b/src/pluralkit/utils.py @@ -11,7 +11,8 @@ from pluralkit.errors import InvalidAvatarURLError def fix_time(time: datetime): - # Assume we're receiving a naive datetime set to UTC, returns naive time zone set to local + """Convert a naive datetime from UTC to local time. humanize's methods expect a local naive time and not a time in UTC.""" + # TODO: replace with methods that call humanize directly, to hide implementation details return time.replace(tzinfo=timezone.utc).astimezone().replace(tzinfo=None)