WIP adding timezone to Cron triggers

pull/145/head
bobloy 4 years ago
parent d619c9a502
commit d377461602

@ -1,8 +1,8 @@
from datetime import datetime from datetime import datetime, tzinfo
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
from apscheduler.triggers.cron import CronTrigger from apscheduler.triggers.cron import CronTrigger
from dateutil import parser from dateutil import parser, tz
from discord.ext.commands import BadArgument, Converter from discord.ext.commands import BadArgument, Converter
from fifo.timezones import assemble_timezones from fifo.timezones import assemble_timezones
@ -11,6 +11,17 @@ if TYPE_CHECKING:
DatetimeConverter = datetime DatetimeConverter = datetime
CronConverter = str CronConverter = str
else: else:
class TimezoneConverter(Converter):
async def convert(self, ctx, argument) -> tzinfo:
tzinfos = assemble_timezones()
if argument.upper() in tzinfos:
return tzinfos[argument.upper()]
timez = tz.gettz(argument)
if timez is not None:
return timez
raise BadArgument()
class DatetimeConverter(Converter): class DatetimeConverter(Converter):
async def convert(self, ctx, argument) -> datetime: async def convert(self, ctx, argument) -> datetime:

@ -11,7 +11,7 @@ from redbot.core import Config, checks, commands
from redbot.core.bot import Red from redbot.core.bot import Red
from redbot.core.commands import TimedeltaConverter from redbot.core.commands import TimedeltaConverter
from .datetime_cron_converters import CronConverter, DatetimeConverter from .datetime_cron_converters import CronConverter, DatetimeConverter, TimezoneConverter
from .task import Task from .task import Task
schedule_log = logging.getLogger("red.fox_v3.fifo.scheduler") schedule_log = logging.getLogger("red.fox_v3.fifo.scheduler")
@ -444,7 +444,12 @@ class FIFO(commands.Cog):
@fifo_trigger.command(name="cron") @fifo_trigger.command(name="cron")
async def fifo_trigger_cron( async def fifo_trigger_cron(
self, ctx: commands.Context, task_name: str, *, cron_str: CronConverter self,
ctx: commands.Context,
task_name: str,
optional_tz: Optional[TimezoneConverter] = None,
*,
cron_str: CronConverter,
): ):
""" """
Add a cron "time of day" trigger to the specified task Add a cron "time of day" trigger to the specified task
@ -460,7 +465,7 @@ class FIFO(commands.Cog):
) )
return return
result = await task.add_trigger("cron", cron_str) result = await task.add_trigger("cron", cron_str, optional_tz)
if not result: if not result:
await ctx.maybe_send_embed( await ctx.maybe_send_embed(
"Failed to add a cron trigger to this task, see console for logs" "Failed to add a cron trigger to this task, see console for logs"

@ -300,7 +300,10 @@ class Task:
self.data["command_str"] = command_str self.data["command_str"] = command_str
return True return True
async def add_trigger(self, param, parsed_time: Union[timedelta, datetime, str]): async def add_trigger(
self, param, parsed_time: Union[timedelta, datetime, str], timezone=None
):
# TODO: Save timezone separately for cron and date triggers
trigger_data = {"type": param, "time_data": parsed_time} trigger_data = {"type": param, "time_data": parsed_time}
if not get_trigger(trigger_data): if not get_trigger(trigger_data):
return False return False

Loading…
Cancel
Save