Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ build/
dist/
*.egg-info/
.tox/
.env/
.idea/
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
click==4.0
redis==2.10.5
structlog==15.1.0
croniter==0.3.17
5 changes: 4 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
install_requires = [
'click',
'redis',
'structlog'
'structlog',
'croniter',
'pytz',
'tzlocal',
]

tests_require = install_requires + [
Expand Down
18 changes: 17 additions & 1 deletion tasktiger/schedule.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import datetime
import pytz
import tzlocal
import croniter

__all__ = ['periodic']
__all__ = ['periodic', 'cron_expr']

def _periodic(dt, period, start_date, end_date):
if end_date and dt >= end_date:
Expand Down Expand Up @@ -39,3 +42,16 @@ def periodic(seconds=0, minutes=0, hours=0, days=0, weeks=0, start_date=None,
# Saturday at midnight
start_date = datetime.datetime(2000, 1, 1)
return (_periodic, (period, start_date, end_date))

def _cron_expr(naive_utc_dt, expr):
aware_utc_dt = pytz.utc.localize(naive_utc_dt)
local_timezone = tzlocal.get_localzone()
local_dt = aware_utc_dt.astimezone(local_timezone)
local_dt = local_timezone.normalize(local_dt)
next_dt = croniter.croniter(expr, start_time=local_dt).get_next(ret_type=datetime.datetime)
next_utc = next_dt.astimezone(pytz.utc)
next_utc = pytz.utc.normalize(next_utc)
return next_utc

def cron_expr(expr_format):
return _cron_expr, (expr_format, )