[Python] I am working on a scheduling system in Python that adheres to the following rules

Discussão em 'Python' iniciado por Stack, Janeiro 17, 2025.

  1. Stack

    Stack Membro Participativo

    I am working on a scheduling system in Python that adheres to the following rules:

    • Each day has a fixed time window: 9:00 AM to 1:00 PM (in the local timezone of the user).
    • If the current time falls outside this window:
      • Before 9:00 AM: Adjust to 9:00 AM + interval.
      • After 1:00 PM: Schedule for the next day's 9:00 AM + interval.
    • The scheduling system should only work for 3 consecutive days.
    • If the scheduling exceeds the third day’s window (1:00 PM), it should stop or cap at 1:00 PM on the third day.
    • I need to calculate the next scheduled time in UTC, but all comparisons must be done in the user’s local timezone. I’ve implemented a function, but the logic for transitioning to the next day’s window and stopping at the third day doesn’t work correctly.

    Here’s the core of my current implementation:

    def get_next_scheduled_time(country, interval_seconds=20):
    tz_name = time_zone_mapping.get(country, "UTC")
    timezone = pytz.timezone(tz_name)

    now_local = datetime.now(pytz.utc).astimezone(timezone)

    base_time = timezone.localize(datetime(now_local.year, now_local.month, now_local.day, 9, 0, 0))
    end_time = timezone.localize(datetime(now_local.year, now_local.month, now_local.day, 13, 0, 0))

    staggered_time = now_local + timedelta(seconds=interval_seconds)

    if staggered_time < base_time:
    staggered_time = base_time + timedelta(seconds=interval_seconds)
    elif base_time <= staggered_time <= end_time:
    staggered_time = now_local + timedelta(seconds=interval_seconds)
    if staggered_time > end_time:
    staggered_time = base_time + timedelta(days=1) + timedelta(seconds=interval_seconds)
    else:
    staggered_time = base_time + timedelta(days=1) + timedelta(seconds=interval_seconds)

    max_scheduled_time = datetime.now(pytz.utc) + timedelta(days=3)
    if staggered_time.astimezone(pytz.utc) > max_scheduled_time:
    staggered_time = timezone.localize(max_scheduled_time).astimezone(timezone)

    return staggered_time.astimezone(pytz.utc).strftime("%Y-%m-%dT%H:%M:%S+00:00")


    The transition to the next day does not always align with the start of the time window (9:00 AM). After transitioning, it sometimes skips or overlaps with the defined time window. I’m not sure whether I should use timedelta(days=...) or timedelta(hours=...) for day transitions to maintain precise control.

    Desired Output:

    • If the current time is outside the window on Day 1, schedule it for Day 2’s 9:00 AM.
    • If Day 3’s window is exceeded, cap the scheduling at 1:00 PM on Day 3.
    • All scheduling calculations should respect the local timezone, but the output must be in UTC.

    How can I fix this logic to ensure proper day-by-day transitions and strict adherence to time windows?

    Continue reading...

Compartilhe esta Página