Emma Rizzi

03/14/2022, 9:54 AM
Hello! I have a flow that uses DateTimeParameters, I would like to schedule it with one date being default to the day of the schedule, or first day of the month for example, is there a way to do it through schedules or should I handle this in my code ?

Stéphan Taljaard

03/14/2022, 10:13 AM
Hi Emma I also have ETL flows, where I need to give a start and end date. These are the tasks I wrote to generate the dates from parameters when the task is run:
Copy code
from datetime import datetime
from typing import Tuple

import pendulum
import prefect
from prefect import task

TIMEZONE = pendulum.timezone("Africa/Johannesburg")

def generate_date(date_parameter, hours_delta=None) -> datetime:  # pendulum.datetime.DateTime
    """Generate a date value from parameter inputs"""
    if not hours_delta:
        minutes_delta_log = ""
        if date_parameter == "scheduled_start_time":
            date_ = prefect.context.get("scheduled_start_time")
        elif date_parameter == "yesterday":
            date_ =
        elif date_parameter == "today":
            date_ =
        elif date_parameter == "now":
            date_ =
            date_ = pendulum.parse(date_parameter, tz=TIMEZONE)

        if not isinstance(date_parameter, datetime):
            raise TypeError("date_parameter should be a DateTime object")
        minutes_delta = round(abs(float(hours_delta)) * 60)
        minutes_delta_log = f" - {minutes_delta} minutes"
        date_ = pendulum.instance(date_parameter).subtract(minutes=minutes_delta)

    date_ = date_.in_timezone(TIMEZONE)
    prefect.context.get("logger").info(f"Generated date for `{date_parameter}`{minutes_delta_log}: {date_}")
    return date_

def check_if_start_hours_delta_given(start_hours_delta_parameter):
    return bool(start_hours_delta_parameter)

def generate_start_and_end_dates(
    start_date, start_date_hours_delta, end_date
) -> Tuple[pendulum.DateTime, pendulum.DateTime]:
    Generates a start and end date that can be used in queries.
    The start date can be a fixed value, or amount of hours to offset the end date with.
    If `hours` is given, it will be used instead of a fixed start date.
    date_end =
    use_start_hours_delta =

    if use_start_hours_delta:
        prefect.context.get("logger").info("Using hours delta")
        date_start =, start_date_hours_delta)
        date_start =

    return date_start, date_end
I then use
in all my flows
upvote 1
🦜 1

Anna Geller

03/14/2022, 10:13 AM
While it's possible to retrieve the date of your schedule using the context:
Copy code
You need to be careful to call it only within a task since the context information is populated at runtime, while the Flow gets built at registration time. That's why passing it as the default parameter value may not work as you intend it and you may need to pass it as data dependency as here:
Copy code
import prefect
from prefect import Flow, task
from prefect.core.parameter import DateTimeParameter

def get_scheduled_start_time():
    return prefect.context.get("scheduled_start_time")

def print_val(x):

def determine_parameter_to_use(sched_start_time, dt_param_val):
    if dt_param_val:

with Flow("parametrized_dt_flow") as flow:
    sched_start_time = get_scheduled_start_time()
    dt_param_value = DateTimeParameter("some_dt", required=False)
    determine_parameter_to_use(sched_start_time, dt_param_value)

if __name__ == "__main__":
🦜 1
❤️ 1

Emma Rizzi

03/14/2022, 10:45 AM
@Stéphan Taljaard @Anna Geller thank you both for your inputs ! looks like exactly what I'm trying to do I didnt know about the pendulum library also, it's very interesting thanks for sharing!
👍 2