refac
This commit is contained in:
1 parent
48ee357156
commit
c4ae8c8678
1 file changed
+20
-19
@@ -73,30 +73,31 @@ def _resolve_tz(tz: str = None) -> Optional[ZoneInfo]:
|
|||||||
def _parse_rule(s: str, now: Optional[datetime] = None):
|
def _parse_rule(s: str, now: Optional[datetime] = None):
|
||||||
"""Parse RRULE with clock-aligned DTSTART for sub-daily frequencies.
|
"""Parse RRULE with clock-aligned DTSTART for sub-daily frequencies.
|
||||||
|
|
||||||
MINUTELY/HOURLY rules use a fixed epoch DTSTART (2000-01-01 00:00)
|
SECONDLY/MINUTELY/HOURLY rules use a fixed epoch DTSTART (2000-01-01 00:00)
|
||||||
so intervals snap to clock boundaries (e.g. every 5min = :00, :05, :10).
|
so intervals snap to clock boundaries (e.g. every 5min = :00, :05, :10).
|
||||||
"""
|
"""
|
||||||
raw = s.replace('RRULE:', '')
|
rrule_line = next((line for line in s.splitlines() if line.upper().startswith('RRULE:')), s)
|
||||||
parts = dict(p.split('=', 1) for p in raw.split(';') if '=' in p)
|
raw = rrule_line.split(':', 1)[1] if rrule_line.upper().startswith('RRULE:') else rrule_line
|
||||||
|
parts = {k.upper(): v for k, v in (p.split('=', 1) for p in raw.split(';') if '=' in p)}
|
||||||
freq = parts.get('FREQ', '')
|
freq = parts.get('FREQ', '')
|
||||||
|
|
||||||
if freq in ('MINUTELY', 'HOURLY'):
|
if freq in ('SECONDLY', 'MINUTELY', 'HOURLY'):
|
||||||
epoch = datetime(2000, 1, 1, 0, 0, 0)
|
epoch = datetime(2000, 1, 1, 0, 0, 0)
|
||||||
if (
|
anchor = now or datetime.now()
|
||||||
now is not None
|
rule = '\n'.join(line for line in s.splitlines() if not line.upper().startswith('DTSTART')) or s
|
||||||
and s.startswith('RRULE:')
|
try:
|
||||||
and '\n' not in s
|
interval = int(parts.get('INTERVAL', '1'))
|
||||||
and '\r' not in s
|
if interval > 0:
|
||||||
and set(parts) <= {'FREQ', 'INTERVAL', 'BYMINUTE', 'BYSECOND'}
|
if freq == 'SECONDLY':
|
||||||
):
|
step = timedelta(seconds=interval)
|
||||||
try:
|
elif freq == 'MINUTELY':
|
||||||
interval = int(parts.get('INTERVAL', '1'))
|
step = timedelta(minutes=interval)
|
||||||
if interval > 0:
|
else:
|
||||||
step = timedelta(minutes=interval) if freq == 'MINUTELY' else timedelta(hours=interval)
|
step = timedelta(hours=interval)
|
||||||
return rrulestr(s, dtstart=epoch + ((now - epoch) // step) * step, ignoretz=True)
|
anchor = epoch + ((anchor - epoch) // step) * step
|
||||||
except (TypeError, ValueError):
|
except (TypeError, ValueError):
|
||||||
pass
|
pass
|
||||||
return rrulestr(s, dtstart=epoch, ignoretz=True)
|
return rrulestr(rule, dtstart=anchor, ignoretz=True)
|
||||||
return rrulestr(s, ignoretz=True)
|
return rrulestr(s, ignoretz=True)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user