-
Notifications
You must be signed in to change notification settings - Fork 964
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add timezone support for .at() (#517)
Co-authored-by: Chris Mahoney <44449504+chrimaho@users.noreply.github.com>
- Loading branch information
1 parent
2f23c12
commit 3eac646
Showing
11 changed files
with
178 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.toctree-l1 { | ||
padding-bottom: 4px; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
Timezone & Daylight Saving Time | ||
=============================== | ||
|
||
Timezone in .at() | ||
~~~~~~~~~~~~~~~~~ | ||
|
||
Schedule supports setting the job execution time in another timezone using the ``.at`` method. | ||
|
||
**To work with timezones** `pytz <https://pypi.org/project/pytz/>`_ **must be installed!** Get it: | ||
|
||
.. code-block:: bash | ||
pip install pytz | ||
Timezones are only available in the ``.at`` function, like so: | ||
|
||
.. code-block:: python | ||
# Pass a timezone as a string | ||
schedule.every().day.at("12:42", "Europe/Amsterdam").do(job) | ||
# Pass an pytz timezone object | ||
from pytz import timezone | ||
schedule.every().friday.at("12:42", timezone("Africa/Lagos")).do(job) | ||
Schedule uses the timezone to calculate the next runtime in local time. | ||
All datetimes inside the library are stored `naive <https://docs.python.org/3/library/datetime.html>`_. | ||
This causes the ``next_run`` and ``last_run`` to always be in Pythons local timezone. | ||
|
||
Daylight Saving Time | ||
~~~~~~~~~~~~~~~~~~~~ | ||
When scheduling jobs with relative time (that is when not using ``.at()``), daylight saving time (DST) is **not** taken into account. | ||
A job that is set to run every 4 hours might execute after 3 realtime hours when DST goes into effect. | ||
This is because schedule is timezone-unaware for relative times. | ||
|
||
However, when using ``.at()``, DST **is** handed correctly: the job will always run at (or close after) the set timestamp. | ||
A job scheduled during a moment that is skipped, the job will execute after the clock is moved. | ||
For example, a job is scheduled ``.at("02:30")``, clock moves from ``02:00`` to ``03:00``, the job will run at ``03:00``. | ||
|
||
Example | ||
~~~~~~~ | ||
Let's say we are in ``Europe/Berlin`` and local datetime is ``2022 march 20, 10:00:00``. | ||
At the moment daylight saving time is not in effect in Berlin (UTC+1). | ||
|
||
We schedule a job to run every day at 10:30:00 in America/New_York. | ||
At this time, daylight saving time is in effect in New York (UTC-4). | ||
|
||
.. code-block:: python | ||
s = every().day.at("10:30", "America/New_York").do(job) | ||
Because of the 5 hour time difference between Berlin and New York the job should effectively run at ``15:30:00``. | ||
So the next run in Berlin time is ``2022 march 20, 15:30:00``: | ||
|
||
.. code-block:: python | ||
print(s.next_run) | ||
# 2022-03-20 15:30:00 | ||
print(repr(s)) | ||
# Every 1 day at 10:30:00 do job() (last run: [never], next run: 2022-03-20 15:30:00) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,4 +7,6 @@ pytest-flake8 | |
Sphinx | ||
black==20.8b1 | ||
click==8.0.4 | ||
mypy | ||
mypy | ||
pytz | ||
types-pytz |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters