Skip to content

Commit 0b9591e

Browse files
authored
Merge pull request #168 from bdrung/use-timestamp
Let _calc take a datetime object instead of a timestamp
2 parents cb114bc + 1a15e97 commit 0b9591e

1 file changed

Lines changed: 16 additions & 15 deletions

File tree

src/croniter/croniter.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,7 @@ def _get_next(
358358
raise TypeError("Invalid ret_type, only 'float' or 'datetime' is acceptable.")
359359

360360
result = self._calc_next(is_prev)
361+
result = self.datetime_to_timestamp(result)
361362

362363
# DST Handling for cron job spanning across days
363364
dtstarttime = self._timestamp_to_datetime(self.dst_start_time)
@@ -440,7 +441,8 @@ def __iter__(self):
440441

441442
__next__ = next = _get_next
442443

443-
def _calc_next(self, is_prev: bool) -> float:
444+
def _calc_next(self, is_prev: bool) -> datetime.datetime:
445+
current = self.timestamp_to_datetime(self.cur)
444446
expanded = self.expanded[:]
445447
nth_weekday_of_month = self.nth_weekday_of_month.copy()
446448

@@ -458,33 +460,32 @@ def _calc_next(self, is_prev: bool) -> float:
458460
else:
459461
bak = expanded[DOW_FIELD]
460462
expanded[DOW_FIELD] = ["*"]
461-
t1 = self._calc(self.cur, expanded, nth_weekday_of_month, is_prev)
463+
t1 = self._calc(current, expanded, nth_weekday_of_month, is_prev)
462464
expanded[DOW_FIELD] = bak
463465
expanded[DAY_FIELD] = ["*"]
464466

465-
t2 = self._calc(self.cur, expanded, nth_weekday_of_month, is_prev)
467+
t2 = self._calc(current, expanded, nth_weekday_of_month, is_prev)
466468
if is_prev:
467469
return t1 if t1 > t2 else t2
468470
return t1 if t1 < t2 else t2
469471

470-
return self._calc(self.cur, expanded, nth_weekday_of_month, is_prev)
472+
return self._calc(current, expanded, nth_weekday_of_month, is_prev)
471473

472474
def _calc(self, now, expanded, nth_weekday_of_month, is_prev):
473475
if is_prev:
474-
now = math.ceil(now)
475476
nearest_diff_method = self._get_prev_nearest_diff
476-
sign = -1
477-
offset = 1 if (len(expanded) > UNIX_CRON_LEN or now % 60 > 0) else 60
477+
offset = relativedelta(microseconds=-1)
478478
else:
479-
now = math.floor(now)
480479
nearest_diff_method = self._get_next_nearest_diff
481-
sign = 1
482-
offset = 1 if (len(expanded) > UNIX_CRON_LEN) else 60
483-
484-
dst = now = self.timestamp_to_datetime(now + sign * offset)
480+
offset = relativedelta(seconds=1) if len(expanded) > UNIX_CRON_LEN else relativedelta(minutes=1)
481+
dst = now + offset
482+
if len(expanded) > UNIX_CRON_LEN:
483+
dst = dst.replace(microsecond=0)
484+
else:
485+
dst = dst.replace(second=0, microsecond=0)
485486

486-
month, year = dst.month, dst.year
487-
current_year = now.year
487+
month = dst.month
488+
year = current_year = dst.year
488489

489490
def proc_year(d):
490491
if len(expanded) == YEAR_CRON_LEN:
@@ -677,7 +678,7 @@ def proc_second(d):
677678
break
678679
if next:
679680
continue
680-
return self.datetime_to_timestamp(dst.replace(microsecond=0))
681+
return dst.replace(microsecond=0)
681682

682683
if is_prev:
683684
raise CroniterBadDateError("failed to find prev date")

0 commit comments

Comments
 (0)