""" Cron utilities for handling day-of-week conversion """ import re def convert_unix_to_iso_dayofweek(cron_expr: str) -> str: """ Convert Unix cron day-of-week numbering (0=Sunday) to ISO numbering (0=Monday) Unix cron standard: 0 = Sunday, 1 = Monday, 2 = Tuesday, 3 = Wednesday, 4 = Thursday, 5 = Friday, 6 = Saturday ISO standard (used by APScheduler): 0 = Monday, 1 = Tuesday, 2 = Wednesday, 3 = Thursday, 4 = Friday, 5 = Saturday, 6 = Sunday Conversion mapping: Unix -> ISO 0 (Sun) -> 6 1 (Mon) -> 0 2 (Tue) -> 1 3 (Wed) -> 2 4 (Thu) -> 3 5 (Fri) -> 4 6 (Sat) -> 5 Args: cron_expr: 5-field cron expression (e.g., "30 12 * * 3") Returns: Converted cron expression with ISO day-of-week numbering """ parts = cron_expr.strip().split() # Must be 5 fields if len(parts) != 5: return cron_expr minute, hour, day, month, weekday = parts # If weekday field contains only letters (day names), no conversion needed if weekday.replace(',', '').replace('-', '').isalpha(): return cron_expr # If weekday is *, no conversion needed if weekday == '*': return cron_expr # Convert numeric day-of-week from Unix to ISO def convert_day(day_str: str) -> str: """Convert a single day number or range""" # Unix to ISO mapping unix_to_iso = { '0': '6', # Sun -> 6 '1': '0', # Mon -> 0 '2': '1', # Tue -> 1 '3': '2', # Wed -> 2 '4': '3', # Thu -> 3 '5': '4', # Fri -> 4 '6': '5', # Sat -> 5 } # Handle ranges like "1-5" if '-' in day_str: start, end = day_str.split('-') if start in unix_to_iso and end in unix_to_iso: return f"{unix_to_iso[start]}-{unix_to_iso[end]}" # Handle single numbers return unix_to_iso.get(day_str, day_str) # Handle comma-separated days like "1,3,5" if ',' in weekday: converted_days = [convert_day(d.strip()) for d in weekday.split(',')] weekday = ','.join(converted_days) else: weekday = convert_day(weekday) # Reconstruct cron expression return f"{minute} {hour} {day} {month} {weekday}"