Minor nitpicks in the fronthistory command
This commit is contained in:
parent
292528ae47
commit
345e8baab6
@ -1,6 +1,6 @@
|
|||||||
import dateparser
|
import dateparser
|
||||||
import humanize
|
import humanize
|
||||||
from datetime import datetime
|
from datetime import datetime, timedelta
|
||||||
|
|
||||||
import pluralkit.utils
|
import pluralkit.utils
|
||||||
from pluralkit.bot import help
|
from pluralkit.bot import help
|
||||||
@ -172,14 +172,20 @@ async def system_frontpercent(ctx: CommandContext):
|
|||||||
system = await ctx.ensure_system()
|
system = await ctx.ensure_system()
|
||||||
|
|
||||||
# Parse the time limit (will go this far back)
|
# Parse the time limit (will go this far back)
|
||||||
before = dateparser.parse(ctx.remaining(), languages=["en"], settings={
|
if ctx.remaining():
|
||||||
"TO_TIMEZONE": "UTC",
|
before = dateparser.parse(ctx.remaining(), languages=["en"], settings={
|
||||||
"RETURN_AS_TIMEZONE_AWARE": False
|
"TO_TIMEZONE": "UTC",
|
||||||
})
|
"RETURN_AS_TIMEZONE_AWARE": False
|
||||||
|
})
|
||||||
|
|
||||||
# If time is in the future, just kinda discard
|
if not before:
|
||||||
if before and before > datetime.utcnow():
|
return CommandError("Could not parse '{}' as a valid time.".format(ctx.remaining()))
|
||||||
before = None
|
|
||||||
|
# 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
|
# Fetch list of switches
|
||||||
all_switches = await pluralkit.utils.get_front_history(ctx.conn, system.id, 99999)
|
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
|
# Calculate percent
|
||||||
fraction = front_time / total_time
|
fraction = front_time / total_time
|
||||||
percent = int(fraction * 100)
|
percent = round(fraction * 100)
|
||||||
|
|
||||||
embed.add_field(name=member.name if member else "(no fronter)",
|
embed.add_field(name=member.name if member else "(no fronter)",
|
||||||
value="{}% ({})".format(percent, humanize.naturaldelta(front_time)))
|
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)
|
await ctx.reply(embed=embed)
|
||||||
|
@ -11,7 +11,8 @@ from pluralkit.errors import InvalidAvatarURLError
|
|||||||
|
|
||||||
|
|
||||||
def fix_time(time: datetime):
|
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)
|
return time.replace(tzinfo=timezone.utc).astimezone().replace(tzinfo=None)
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user