| 1 |
Timezone & Daylight Saving Time |
| 2 |
=============================== |
| 3 |
|
| 4 |
Timezone in .at() |
| 5 |
~~~~~~~~~~~~~~~~~ |
| 6 |
|
| 7 |
Schedule supports setting the job execution time in another timezone using the ``.at`` method. |
| 8 |
|
| 9 |
**To work with timezones** `pytz <https://pypi.org/project/pytz/>`_ **must be installed!** Get it: |
| 10 |
|
| 11 |
.. code-block:: bash |
| 12 |
|
| 13 |
pip install pytz |
| 14 |
|
| 15 |
Timezones are only available in the ``.at`` function, like so: |
| 16 |
|
| 17 |
.. code-block:: python |
| 18 |
|
| 19 |
# Pass a timezone as a string |
| 20 |
schedule.every().day.at("12:42", "Europe/Amsterdam").do(job) |
| 21 |
|
| 22 |
# Pass an pytz timezone object |
| 23 |
from pytz import timezone |
| 24 |
schedule.every().friday.at("12:42", timezone("Africa/Lagos")).do(job) |
| 25 |
|
| 26 |
Schedule uses the timezone to calculate the next runtime in local time. |
| 27 |
All datetimes inside the library are stored `naive <https://docs.python.org/3/library/datetime.html>`_. |
| 28 |
This causes the ``next_run`` and ``last_run`` to always be in Pythons local timezone. |
| 29 |
|
| 30 |
Daylight Saving Time |
| 31 |
~~~~~~~~~~~~~~~~~~~~ |
| 32 |
Scheduling jobs that do not specify a timezone do **not** take clock-changes into account. |
| 33 |
Timezone unaware jobs will use naive local times to calculate the next run. |
| 34 |
For example, a job that is set to run every 4 hours might execute after 3 realtime hours when DST goes into effect. |
| 35 |
|
| 36 |
But when passing a timezone to ``.at()``, DST **is** taken into account. |
| 37 |
The job will run at the specified time, even when the clock changes. |
| 38 |
|
| 39 |
Example clock moves forward: |
| 40 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 41 |
When a job is scheduled in the gap that occurs when the clock moves forward, the job is scheduled after the gap. |
| 42 |
|
| 43 |
A job is scheduled ``.at("02:30", "Europe/Berlin")``. |
| 44 |
When the clock moves from ``02:00`` to ``03:00``, the job will run once at ``03:30``. |
| 45 |
The day after it will return to normal and run at ``02:30``. |
| 46 |
|
| 47 |
A job is scheduled ``.at("01:00", "Europe/London")``. |
| 48 |
When the clock moves from ``01:00`` to ``02:00``, the job will run once at ``02:00``. |
| 49 |
|
| 50 |
Example clock moves backwards: |
| 51 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 52 |
A job is scheduled ``.at("02:30", "Europe/Berlin")``. |
| 53 |
When the clock moves from ``02:00`` to ``03:00``, the job will run once at ``02:30``. |
| 54 |
It will run only at the first time the clock hits ``02:30``, but not the second time. |
| 55 |
The day after, it will return to normal and run at ``02:30``. |
| 56 |
|
| 57 |
Example scheduling across timezones |
| 58 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 59 |
Let's say we are in ``Europe/Berlin`` and local datetime is ``2022 march 20, 10:00:00``. |
| 60 |
At this moment daylight saving time is not in effect in Berlin (UTC+1). |
| 61 |
|
| 62 |
We schedule a job to run every day at 10:30:00 in America/New_York. |
| 63 |
At this time, daylight saving time is in effect in New York (UTC-4). |
| 64 |
|
| 65 |
.. code-block:: python |
| 66 |
|
| 67 |
s = every().day.at("10:30", "America/New_York").do(job) |
| 68 |
|
| 69 |
Because of the 5 hour time difference between Berlin and New York the job should effectively run at ``15:30:00``. |
| 70 |
So the next run in Berlin time is ``2022 march 20, 15:30:00``: |
| 71 |
|
| 72 |
.. code-block:: python |
| 73 |
|
| 74 |
print(s.next_run) |
| 75 |
# 2022-03-20 15:30:00 |
| 76 |
|
| 77 |
print(repr(s)) |
| 78 |
# Every 1 day at 10:30:00 do job() (last run: [never], next run: 2022-03-20 15:30:00) |