File size: 3,786 Bytes
046723b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
from datetime import timedelta, datetime
from enum import IntEnum
from zoneinfo import ZoneInfo


class Weekday(IntEnum):
    """Enumeration for days of the week."""
    Monday = 0
    Tuesday = 1
    Wednesday = 2
    Thursday = 3
    Friday = 4
    Saturday = 5
    Sunday = 6


def am_i_inside_time(
        day_of_week: str,
        time_str: str,
        timezone_str: str,
        duration: int = 15,
) -> bool:
    """
    Determines if the current time falls within a specified time range.

    Parameters:
        day_of_week (str): The day of the week (e.g., 'Monday').
        time_str (str): The start time in 'HH:MM' format.
        timezone_str (str): The timezone identifier (e.g., 'Europe/Berlin').
        duration (int, optional): The duration of the time range in minutes. Default is 15.

    Returns:
        bool: True if the current time is within the time range, False otherwise.
    """
    # Parse the target day of the week
    try:
        target_weekday = Weekday[day_of_week.capitalize()]
    except KeyError:
        raise ValueError(f"Invalid day_of_week: '{day_of_week}'. Must be a valid weekday name.")

    # Parse the start time
    try:
        target_time = datetime.strptime(time_str, '%H:%M').time()
    except ValueError:
        raise ValueError(f"Invalid time_str: '{time_str}'. Must be in 'HH:MM' format.")

    # Define the timezone
    try:
        tz = ZoneInfo(timezone_str)
    except Exception:
        raise ValueError(f"Invalid timezone_str: '{timezone_str}'. Must be a valid timezone identifier.")

    # Get the current time in the specified timezone
    now_tz = datetime.now(tz)

    # Check if the current day matches the target day or overlaps due to duration
    current_weekday = now_tz.weekday()
    start_datetime_tz = datetime.combine(now_tz.date(), target_time, tzinfo=tz)

    # Handle previous day's overlap
    if target_weekday == (current_weekday - 1) % 7:
        # Calculate start and end times for the overlap from the previous day
        start_datetime_tz -= timedelta(days=1)
        end_datetime_tz = start_datetime_tz + timedelta(minutes=duration)
        if start_datetime_tz <= now_tz < end_datetime_tz:
            return True

    # Handle current day's range
    if target_weekday == current_weekday:
        end_datetime_tz = start_datetime_tz + timedelta(minutes=duration)
        if start_datetime_tz <= now_tz < end_datetime_tz:
            return True

    # Handle next day's overlap
    if target_weekday == (current_weekday + 1) % 7:
        end_datetime_tz = start_datetime_tz + timedelta(minutes=duration)
        if now_tz < start_datetime_tz and now_tz + timedelta(days=1) < end_datetime_tz:
            return True

    return False


def is_within_schedule(time_schedule_limit, default_tz="UTC"):
    if time_schedule_limit and time_schedule_limit.get('enabled'):
        # Get the timezone the time schedule is in, so we know what day it is there
        tz_name = time_schedule_limit.get('timezone')
        if not tz_name:
            tz_name = default_tz

        now_day_name_in_tz = datetime.now(ZoneInfo(tz_name.strip())).strftime('%A')
        selected_day_schedule = time_schedule_limit.get(now_day_name_in_tz.lower())
        if not selected_day_schedule.get('enabled'):
            return False

        duration = selected_day_schedule.get('duration')
        selected_day_run_duration_m = int(duration.get('hours')) * 60 + int(duration.get('minutes'))

        is_valid = am_i_inside_time(day_of_week=now_day_name_in_tz,
                                    time_str=selected_day_schedule['start_time'],
                                    timezone_str=tz_name,
                                    duration=selected_day_run_duration_m)

        return is_valid

    return False