Skip to main content

Quick Start

This is a python implementation of ChronoX. It requires using cronx, a cron like expression, see cronx guide.

Github repo here

Install

pip install chronox-python

python >= 3.8

Usage

This package provides two main classes: ChronoX for time point and ChronoXSpan for time span.

The input and output should be encapsulated by datetime class from datetime module;

from datetime import datetime

Time Point

ChronoX class provides 3 main functions: prev and next to calculate previous and next time point respectively and contains to check if the passed in datetime can be represented by the pattern.


from chronox import ChronoX
from datetime import datetime

cron = ChronoX("* * * 1,3,5 * * ; c")

assert cron.prev(datetime(2003, 11, 10, 6, 0, 6)) == datetime(2003, 11, 10, 5, 59, 59)
assert cron.prev(datetime(2003, 11, 10, 6, 0, 6), 1) == datetime(2003, 11, 10, 5, 59, 59)
assert cron.prev(datetime(2003, 11, 10, 6, 0, 6), leap=1) == datetime(2003, 11, 10, 5, 59, 59)

assert cron.prev(datetime(2003, 11, 10, 0, 0, 6), leap=10) == datetime(2003, 11, 9, 5, 59, 50)

assert cron.next(datetime(2003, 11, 10, 5, 59, 59)) == datetime(2003, 11, 11, 1, 0, 0)

# If current datetime represented by the cron
cron.contains(datetime.now())

# is equivalent to

datetime.now() in cron

info

parameter for datetime is optional and default to datetime.now()

Time Span

ChronoXSpan provides a main function contains. It also provide start and end properties which reference decoded ChronoX instances for start and end pattern, you can utilize these two properties for calculation releted start or end pattern seperately.

from chronox import ChronoXSpan
from datetime import datetime

period = ChronoXSpan("* 1,3,5 * 3..5 0 0 0; m")

assert period.contains(datetime(2003, 5, 16, 0, 0, 0))

# is equivalent to

datetime(2003, 5, 16, 0, 0, 0).now() in period

# calculate next start time
period.start.next()

# calculate next end time
period.end.next()