Fixed croniter timezone offset logic mismatch

This commit is contained in:
Fabio Manganiello 2020-11-21 01:36:37 +01:00
parent 0c0e7411f7
commit 9dacd2d3c9
2 changed files with 11 additions and 8 deletions

View file

@ -1,4 +1,5 @@
import datetime import datetime
import enum
import os import os
import time import time
import threading import threading
@ -6,7 +7,7 @@ import threading
from typing import Optional, Union, Dict, Any, List from typing import Optional, Union, Dict, Any, List
import croniter import croniter
import enum from dateutil.tz import gettz
from platypush.backend import Backend from platypush.backend import Backend
from platypush.context import get_bus, get_plugin from platypush.context import get_bus, get_plugin
@ -54,18 +55,19 @@ class Alarm:
self._runtime_snooze_interval = snooze_interval self._runtime_snooze_interval = snooze_interval
def get_next(self) -> float: def get_next(self) -> float:
now = time.time() now = datetime.datetime.now().replace(tzinfo=gettz())
try: try:
cron = croniter.croniter(self.when, now) cron = croniter.croniter(self.when, now)
return cron.get_next() return cron.get_next()
except (AttributeError, croniter.CroniterBadCronError): except (AttributeError, croniter.CroniterBadCronError):
try: try:
timestamp = datetime.datetime.fromisoformat(self.when).timestamp() timestamp = datetime.datetime.fromisoformat(self.when).replace(tzinfo=gettz())
except (TypeError, ValueError): except (TypeError, ValueError):
timestamp = (datetime.datetime.now() + datetime.timedelta(seconds=int(self.when))).timestamp() timestamp = (datetime.datetime.now().replace(tzinfo=gettz()) +
datetime.timedelta(seconds=int(self.when)))
return timestamp if timestamp >= now else None return timestamp.timestamp() if timestamp >= now else None
def is_enabled(self): def is_enabled(self):
return self._enabled return self._enabled

View file

@ -163,6 +163,7 @@ setup(
'wheel', 'wheel',
'zeroconf>=0.27.0', 'zeroconf>=0.27.0',
'tz', 'tz',
'python-dateutil',
], ],
extras_require={ extras_require={
@ -173,9 +174,9 @@ setup(
# Support for Pushbullet backend and plugin # Support for Pushbullet backend and plugin
'pushbullet': ['pushbullet.py @ https://github.com/rbrcsk/pushbullet.py/tarball/master'], 'pushbullet': ['pushbullet.py @ https://github.com/rbrcsk/pushbullet.py/tarball/master'],
# Support for HTTP backend # Support for HTTP backend
'http': ['flask', 'python-dateutil', 'frozendict', 'bcrypt'], 'http': ['flask', 'frozendict', 'bcrypt'],
# Support for uWSGI HTTP backend # Support for uWSGI HTTP backend
'uwsgi': ['flask', 'python-dateutil', 'frozendict', 'uwsgi', 'bcrypt'], 'uwsgi': ['flask', 'frozendict', 'uwsgi', 'bcrypt'],
# Support for MQTT backends # Support for MQTT backends
'mqtt': ['paho-mqtt'], 'mqtt': ['paho-mqtt'],
# Support for RSS feeds parser # Support for RSS feeds parser
@ -220,7 +221,7 @@ setup(
# Support for serial port plugin # Support for serial port plugin
'serial': ['pyserial'], 'serial': ['pyserial'],
# Support for ICal calendars # Support for ICal calendars
'ical': ['icalendar', 'python-dateutil'], 'ical': ['icalendar'],
# Support for joystick backend # Support for joystick backend
'joystick': ['inputs'], 'joystick': ['inputs'],
# Support for Kodi plugin # Support for Kodi plugin