taichino / croniter

croniter is a python module to provide iteration for datetime object.
http://github.com/taichino/croniter
387 stars 105 forks source link

get_prev() is pointing to wrong date in a period of one second after the cron time #107

Closed DevNils closed 6 years ago

DevNils commented 6 years ago

Version: 0.3.23 System: macOS 10.12.6 Python: 2.7.10

I tested croniter in a very time sensitive application and discovered that get_prev() is pointing to the wrong date in a period of one second after the cron time.

Example:

date = datetime.datetime(
    year=2018,
    month=1,
    day=2,
    hour=10,
    minute=0,
    second=0,
    microsecond=0
)
c = croniter.croniter("0 10 * * * ", start_time=date)
print(datetime.datetime.utcfromtimestamp(c.get_prev()))

--> 2018-01-01 10:00:00

croniter is pointing to the date from the day before.

date = datetime.datetime(
    year=2018,
    month=1,
    day=2,
    hour=10,
    minute=0,
    second=0,
    microsecond=500000
)
c = croniter.croniter("0 10 * * * ", start_time=date)
print(datetime.datetime.utcfromtimestamp(c.get_prev()))

--> 2018-01-01 10:00:00

croniter is still pointing to the day before, but it should point to 2018-01-02 10:00:00

date = datetime.datetime(
    year=2018,
    month=1,
    day=2,
    hour=10,
    minute=0,
    second=1,
    microsecond=0
)
c = croniter.croniter("0 10 * * * ", start_time=date)
print(datetime.datetime.utcfromtimestamp(c.get_prev()))

--> 2018-01-02 10:00:00

Not before 1 second after the date croniter switches to the right one.

Is it possible to increase the time resolution to a millisecond base?

Kind regards

Nils

kiorky commented 6 years ago

Let me re read well, but no, for 0 10 * * *, trigger is only after: 10:01; smallest cron increment is minute (https://en.wikipedia.org/wiki/Cron), so as the trigger.

kiorky commented 6 years ago

Ok, patent bug :)

kiorky commented 6 years ago

Uhm im not sure we can go under the second threshold after all, its counterintuitive.

kiorky commented 6 years ago

0.3.24 is out, thx for the report: https://pypi.org/project/croniter/0.3.24/

DevNils commented 6 years ago

Thanks a lot for this and this very nice module!