diff --git a/CHANGELOG b/CHANGELOG index 98877fe367..ebe61a3335 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,4 @@ +* Schedule actions using server timezone * Reset the user sessions less often * Add straight values to wizard state view * Add strip option to Char fields diff --git a/doc/ref/tools/index.rst b/doc/ref/tools/index.rst index d74ac88241..f0a65bb288 100644 --- a/doc/ref/tools/index.rst +++ b/doc/ref/tools/index.rst @@ -13,3 +13,4 @@ Tools API reference. email singleton immutabledict + timezone diff --git a/doc/ref/tools/timezone.rst b/doc/ref/tools/timezone.rst new file mode 100644 index 0000000000..a7515c45a1 --- /dev/null +++ b/doc/ref/tools/timezone.rst @@ -0,0 +1,25 @@ +.. _ref-tools-timezone: +.. module:: trytond.tools.timezone + +timezone +======== + +.. function:: get_tzinfo(zoneid) + + Get a class representing a IANA time zone specified by the string ``zoneid``. + +.. function:: available_timezones() + + Get a list of all the valid IANA keys available. + +.. attribute:: UTC + + The UTC :py:class:`datetime.tzinfo` instance. + +.. attribute:: SERVER + + The server timezone :py:class:`datetime.tzinfo` instance. + + Tryton tests the environment variables ``TRYTOND_TZ`` and ``TZ`` in this + order to select to IANA key to use. + If they are both empty, it defaults to ``UTC``. diff --git a/doc/topics/cron.rst b/doc/topics/cron.rst index e7b65acaf9..bc4b37de48 100644 --- a/doc/topics/cron.rst +++ b/doc/topics/cron.rst @@ -11,6 +11,11 @@ The planning is managed by ``ir.cron`` records which store the method to call and the interval of time between calls. The method must be a class method of a :class:`~trytond.model.Model` which can be called without any parameters. +.. note:: + + The timezone used to schedule the action is + :attr:`timezone.SERVER `. + To register a new method with the scheduler, you must extend the ``ir.cron`` model and append the new method to the :attr:`~trytond.model.fields.Selection.selection` attribute of the ``method`` diff --git a/setup.py b/setup.py index 83f6df322e..add210718d 100755 --- a/setup.py +++ b/setup.py @@ -161,6 +161,7 @@ setup(name=name, 'werkzeug', 'wrapt', 'passlib >= 1.7.0', + 'pytz;python_verrion<"3.9"', ], extras_require={ 'test': tests_require, diff --git a/trytond/__init__.py b/trytond/__init__.py index a047568d4b..43c8972c67 100644 --- a/trytond/__init__.py +++ b/trytond/__init__.py @@ -9,6 +9,7 @@ from lxml import etree, objectify __version__ = "6.5.0" +os.environ.setdefault('TRYTOND_TZ', os.environ.get('TZ', 'UTC')) os.environ['TZ'] = 'UTC' if hasattr(time, 'tzset'): time.tzset() diff --git a/trytond/ir/cron.py b/trytond/ir/cron.py index 6e586bde58..ab59738923 100644 --- a/trytond/ir/cron.py +++ b/trytond/ir/cron.py @@ -14,6 +14,7 @@ from trytond.model import ( from trytond.pool import Pool from trytond.pyson import Eval from trytond.status import processing +from trytond.tools import timezone as tz from trytond.transaction import Transaction from trytond.worker import run_task @@ -66,6 +67,7 @@ class Cron(DeactivableMixin, ModelSQL, ModelView): ['minutes', 'hours', 'days', 'weeks']), }, depends=['interval_type']) + timezone = fields.Function(fields.Char("Timezone"), 'get_timezone') next_call = fields.DateTime("Next Call", select=True) method = fields.Selection([ @@ -97,6 +99,9 @@ class Cron(DeactivableMixin, ModelSQL, ModelView): # Migration from 5.0: remove required on next_call table_h.not_null_action('next_call', 'remove') + def get_timezone(self, name): + return tz.SERVER.key + @staticmethod def check_xml_record(crons, values): return True @@ -110,7 +115,7 @@ class Cron(DeactivableMixin, ModelSQL, ModelView): ] def compute_next_call(self, now): - return (now + return (now.replace(tzinfo=tz.UTC).astimezone(tz.SERVER) + relativedelta(**{self.interval_type: self.interval_number}) + relativedelta( microsecond=0, @@ -132,7 +137,7 @@ class Cron(DeactivableMixin, ModelSQL, ModelView): int(self.weekday.index) if self.weekday and self.interval_type not in {'minutes', 'hours', 'days'} - else None))) + else None))).astimezone(tz.UTC).replace(tzinfo=None) @dualmethod @ModelView.button diff --git a/trytond/ir/view/cron_form.xml b/trytond/ir/view/cron_form.xml index 97d51e88a8..479250a2b0 100644 --- a/trytond/ir/view/cron_form.xml +++ b/trytond/ir/view/cron_form.xml @@ -21,6 +21,8 @@ this repository contains the full copyright notices and license terms. -->