Use system time zones in pk;switch move parsing

This commit is contained in:
Ske 2018-12-18 20:13:45 +01:00
parent d0e0070f19
commit 8493256e3e

View File

@ -2,6 +2,7 @@ from datetime import datetime
from typing import List from typing import List
import dateparser import dateparser
import pytz
from pluralkit.bot.commands import * from pluralkit.bot.commands import *
from pluralkit.member import Member from pluralkit.member import Member
@ -94,9 +95,17 @@ async def switch_move(ctx: CommandContext):
# Parse the time to move to # Parse the time to move to
new_time = dateparser.parse(ctx.remaining(), languages=["en"], settings={ new_time = dateparser.parse(ctx.remaining(), languages=["en"], settings={
"TO_TIMEZONE": "UTC", # Tell it to default to the system's given time zone
"RETURN_AS_TIMEZONE_AWARE": False # If no time zone was given *explicitly in the string* it'll return as naive
"TIMEZONE": system.ui_tz
}) })
tz = pytz.timezone(system.ui_tz)
# So we default to putting the system's time zone in the tzinfo
if not new_time.tzinfo:
new_time = tz.localize(new_time)
# Now that we have a system-time datetime, convert this to UTC and make it naive since that's what we deal with
new_time = pytz.utc.normalize(new_time).replace(tzinfo=None)
if not new_time: if not new_time:
raise CommandError("'{}' can't be parsed as a valid time.".format(ctx.remaining())) raise CommandError("'{}' can't be parsed as a valid time.".format(ctx.remaining()))