Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _get_next_dt(dt_, options): # noqa: C901
for field in dt_fields:
v = options[field]
if v is None:
continue
if field == D.weekday:
next_v = dt_.weekday()
else:
next_v = getattr(dt_, field)
if isinstance(v, int):
mismatch = next_v != v
else:
assert isinstance(v, (set, list, tuple))
mismatch = next_v not in v
# print(field, v, next_v, mismatch)
if mismatch:
micro = max(dt_.microsecond - options[D.microsecond], 0)
if field == D.month:
if dt_.month == 12:
return datetime(dt_.year + 1, 1, 1)
else:
return datetime(dt_.year, dt_.month + 1, 1)
else:
return datetime(dt_.year, dt_.month + 1, 1)
elif field in (D.day, D.weekday):
return (
dt_
+ timedelta(days=1)
- timedelta(hours=dt_.hour, minutes=dt_.minute, seconds=dt_.second, microseconds=micro)
)
elif field == D.hour:
return dt_ + timedelta(hours=1) - timedelta(minutes=dt_.minute, seconds=dt_.second, microseconds=micro)
elif field == D.minute:
return dt_ + timedelta(minutes=1) - timedelta(seconds=dt_.second, microseconds=micro)
elif field == D.second:
return dt_ + timedelta(seconds=1) - timedelta(microseconds=micro)
else:
assert field == D.microsecond, field
return dt_ + timedelta(microseconds=options['microsecond'] - dt_.microsecond)
else:
next_v = getattr(dt_, field)
if isinstance(v, int):
mismatch = next_v != v
else:
assert isinstance(v, (set, list, tuple))
mismatch = next_v not in v
# print(field, v, next_v, mismatch)
if mismatch:
micro = max(dt_.microsecond - options[D.microsecond], 0)
if field == D.month:
if dt_.month == 12:
return datetime(dt_.year + 1, 1, 1)
else:
return datetime(dt_.year, dt_.month + 1, 1)
elif field in (D.day, D.weekday):
return (
dt_
+ timedelta(days=1)
- timedelta(hours=dt_.hour, minutes=dt_.minute, seconds=dt_.second, microseconds=micro)
)
elif field == D.hour:
return dt_ + timedelta(hours=1) - timedelta(minutes=dt_.minute, seconds=dt_.second, microseconds=micro)
elif field == D.minute:
return dt_ + timedelta(minutes=1) - timedelta(seconds=dt_.second, microseconds=micro)
elif field == D.second:
return dt_ + timedelta(seconds=1) - timedelta(microseconds=micro)
else:
assert field == D.microsecond, field
return dt_ + timedelta(microseconds=options['microsecond'] - dt_.microsecond)
if mismatch:
micro = max(dt_.microsecond - options[D.microsecond], 0)
if field == D.month:
if dt_.month == 12:
return datetime(dt_.year + 1, 1, 1)
else:
return datetime(dt_.year, dt_.month + 1, 1)
elif field in (D.day, D.weekday):
return (
dt_
+ timedelta(days=1)
- timedelta(hours=dt_.hour, minutes=dt_.minute, seconds=dt_.second, microseconds=micro)
)
elif field == D.hour:
return dt_ + timedelta(hours=1) - timedelta(minutes=dt_.minute, seconds=dt_.second, microseconds=micro)
elif field == D.minute:
return dt_ + timedelta(minutes=1) - timedelta(seconds=dt_.second, microseconds=micro)
elif field == D.second:
return dt_ + timedelta(seconds=1) - timedelta(microseconds=micro)
else:
assert field == D.microsecond, field
return dt_ + timedelta(microseconds=options['microsecond'] - dt_.microsecond)
v = options[field]
if v is None:
continue
if field == D.weekday:
next_v = dt_.weekday()
else:
next_v = getattr(dt_, field)
if isinstance(v, int):
mismatch = next_v != v
else:
assert isinstance(v, (set, list, tuple))
mismatch = next_v not in v
# print(field, v, next_v, mismatch)
if mismatch:
micro = max(dt_.microsecond - options[D.microsecond], 0)
if field == D.month:
if dt_.month == 12:
return datetime(dt_.year + 1, 1, 1)
else:
return datetime(dt_.year, dt_.month + 1, 1)
elif field in (D.day, D.weekday):
return (
dt_
+ timedelta(days=1)
- timedelta(hours=dt_.hour, minutes=dt_.minute, seconds=dt_.second, microseconds=micro)
)
elif field == D.hour:
return dt_ + timedelta(hours=1) - timedelta(minutes=dt_.minute, seconds=dt_.second, microseconds=micro)
elif field == D.minute:
return dt_ + timedelta(minutes=1) - timedelta(seconds=dt_.second, microseconds=micro)
elif field == D.second:
return dt_ + timedelta(seconds=1) - timedelta(microseconds=micro)
from pydantic.utils import import_string
from arq.utils import SecondsTimedelta, to_seconds
class D(str, Enum):
month = 'month'
day = 'day'
weekday = 'weekday'
hour = 'hour'
minute = 'minute'
second = 'second'
microsecond = 'microsecond'
dt_fields = D.month, D.day, D.weekday, D.hour, D.minute, D.second, D.microsecond
weekdays = 'mon', 'tues', 'wed', 'thurs', 'fri', 'sat', 'sun'
def _get_next_dt(dt_, options): # noqa: C901
for field in dt_fields:
v = options[field]
if v is None:
continue
if field == D.weekday:
next_v = dt_.weekday()
else:
next_v = getattr(dt_, field)
if isinstance(v, int):
mismatch = next_v != v
else:
assert isinstance(v, (set, list, tuple))
if field == D.month:
if dt_.month == 12:
return datetime(dt_.year + 1, 1, 1)
else:
return datetime(dt_.year, dt_.month + 1, 1)
elif field in (D.day, D.weekday):
return (
dt_
+ timedelta(days=1)
- timedelta(hours=dt_.hour, minutes=dt_.minute, seconds=dt_.second, microseconds=micro)
)
elif field == D.hour:
return dt_ + timedelta(hours=1) - timedelta(minutes=dt_.minute, seconds=dt_.second, microseconds=micro)
elif field == D.minute:
return dt_ + timedelta(minutes=1) - timedelta(seconds=dt_.second, microseconds=micro)
elif field == D.second:
return dt_ + timedelta(seconds=1) - timedelta(microseconds=micro)
else:
assert field == D.microsecond, field
return dt_ + timedelta(microseconds=options['microsecond'] - dt_.microsecond)