Minor nitpicks in the fronthistory command

This commit is contained in:
Ske 2018-09-16 19:20:08 +02:00
parent 292528ae47
commit 345e8baab6
2 changed files with 18 additions and 11 deletions

View File

@ -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)
if ctx.remaining():
before = dateparser.parse(ctx.remaining(), languages=["en"], settings={
"TO_TIMEZONE": "UTC",
"RETURN_AS_TIMEZONE_AWARE": False
})
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)

View File

@ -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)