File: usage.rst

package info (click to toggle)
python-time-machine 2.19.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 476 kB
  • sloc: python: 1,826; ansic: 639; makefile: 14
file content (339 lines) | stat: -rw-r--r-- 11,283 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
=====
Usage
=====

.. currentmodule:: time_machine

This document covers time-machine’s API.

.. warning::

    Time is a global state.
    When mocking it, all concurrent threads or asynchronous functions are also affected.
    Some aren't ready for time to move so rapidly or backwards, and may crash or produce unexpected results.

    Also beware that other processes are not affected.
    For example, if you call datetime functions on a database server, they will return the real time.

Main API
========

.. autoclass:: travel

  :param destination:

  :param tick:

  :return:
    ``travel`` instance

  ``travel()`` is a class that allows time travel, to the datetime specified by ``destination``.
  It does so by mocking all functions from Python's standard library that return the current date or datetime.
  It can be used independently, as a function decorator, or as a context manager (synchronous or asynchronous).

  ``destination`` specifies the datetime to move to.
  It may be:

  * A ``datetime.datetime``.
    If it is naive, it will be assumed to have the UTC timezone.
    If it has ``tzinfo`` set to a |zoneinfo-instance|_ or |datetime.UTC|_, the current timezone will also be mocked.
  * A ``datetime.date``.
    This will be converted to a UTC datetime with the time 00:00:00.
  * A ``datetime.timedelta``.
    This will be interpreted relative to the current time.
    If already within a ``travel()`` block, the ``shift()`` method is easier to use (documented below).
  * A ``float`` or ``int`` specifying a `Unix timestamp <https://en.m.wikipedia.org/wiki/Unix_time>`__
  * A string, which will be parsed with `dateutil.parse <https://dateutil.readthedocs.io/en/stable/parser.html>`__ and converted to a timestamp.
    If the result is naive, it will be assumed to be local time.

  .. |zoneinfo-instance| replace:: ``zoneinfo.ZoneInfo`` instance
  .. _zoneinfo-instance: https://docs.python.org/3/library/zoneinfo.html#zoneinfo.ZoneInfo
  .. |datetime.UTC| replace:: ``datetime.UTC`` (``datetime.timezone.utc``)
  .. _datetime.UTC: https://docs.python.org/3/library/datetime.html#datetime.UTC

  Additionally, you can provide some more complex types:

  * A generator, in which case ``next()`` will be called on it, with the result treated as above.
  * A callable, in which case it will be called with no parameters, with the result treated as above.

  ``tick`` defines whether time continues to "tick" after travelling, or is frozen.
  If ``True``, the default, successive calls to mocked functions return values increasing by the elapsed real time *since the first call.*
  So after starting travel to ``0.0`` (the UNIX epoch), the first call to any datetime function will return its representation of ``1970-01-01 00:00:00.000000`` exactly.
  The following calls "tick," so if a call was made exactly half a second later, it would return ``1970-01-01 00:00:00.500000``.

  Mocked functions
  ^^^^^^^^^^^^^^^^

  All datetime functions in the standard library are mocked to move to the destination current datetime:

  * ``datetime.datetime.now()``
  * ``datetime.datetime.utcnow()``
  * ``time.clock_gettime()`` (only for ``CLOCK_REALTIME``)
  * ``time.clock_gettime_ns()`` (only for ``CLOCK_REALTIME``)
  * ``time.gmtime()``
  * ``time.localtime()``
  * ``time.monotonic()`` (not a real monotonic clock, returns ``time.time()``)
  * ``time.monotonic_ns()`` (not a real monotonic clock, returns ``time.time_ns()``)
  * ``time.strftime()``
  * ``time.time()``
  * ``time.time_ns()``

  The mocking is done at the C layer, replacing the function pointers for these built-ins.
  Therefore, it automatically affects everywhere those functions have been imported, unlike use of ``unittest.mock.patch()``.

  Usage with ``start()`` / ``stop()``
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

  To use ``travel()`` independently, use its ``start()`` method to move to the destination time, and ``stop()`` to move back.

  .. automethod:: start

  .. automethod:: stop

  For example:

  .. code-block:: python

      import datetime as dt
      import time_machine

      traveller = time_machine.travel(dt.datetime(1985, 10, 26))
      traveller.start()
      # It's the past!
      assert dt.date.today() == dt.date(1985, 10, 26)
      traveller.stop()
      # We've gone back to the future!
      assert dt.date.today() > dt.date(2020, 4, 29)

  ``travel()`` instances are nestable, but you'll need to be careful when manually managing to call their ``stop()`` methods in the correct order, even when exceptions occur.
  It's recommended to use the decorator or context manager forms instead, to take advantage of Python features to do this.

  Function decorator
  ^^^^^^^^^^^^^^^^^^

  When used as a function decorator, time is mocked during the wrapped function's duration:

  .. code-block:: python

      import time
      import time_machine


      @time_machine.travel("1970-01-01 00:00 +0000")
      def test_in_the_deep_past():
          assert 0.0 < time.time() < 1.0

  You can also decorate asynchronous functions (coroutines):

  .. code-block:: python

      import time
      import time_machine


      @time_machine.travel("1970-01-01 00:00 +0000")
      async def test_in_the_deep_past():
          assert 0.0 < time.time() < 1.0

  .. _travel-context-manager:

  Context manager
  ^^^^^^^^^^^^^^^

  When used as a context manager, time is mocked during the ``with`` block.
  This works both synchronously:

  .. code-block:: python

      import time
      import time_machine


      def test_in_the_deep_past():
          with time_machine.travel(0.0):
              assert 0.0 < time.time() < 1.0

  …and asynchronously:

  .. code-block:: python

      import time
      import time_machine


      async def test_in_the_deep_past():
          async with time_machine.travel(0.0):
              assert 0.0 < time.time() < 1.0

  Class decorator
  ^^^^^^^^^^^^^^^

  Only ``unittest.TestCase`` subclasses are supported.
  When applied as a class decorator to such classes, time is mocked from the start of ``setUpClass()`` to the end of ``tearDownClass()``:

  .. code-block:: python

      import time
      import time_machine
      import unittest


      @time_machine.travel(0.0)
      class DeepPastTests(TestCase):
          def test_in_the_deep_past(self):
              assert 0.0 < time.time() < 1.0

  Note this is different to ``unittest.mock.patch()``\'s behaviour, which is to mock only during the test methods.
  For pytest-style test classes, see the autouse fixture pattern :doc:`in the pytest plugin documentation <pytest_plugin>`.

  Timezone mocking
  ^^^^^^^^^^^^^^^^

  If the ``destination`` passed to ``time_machine.travel()`` or ``Coordinates.move_to()`` has its ``tzinfo`` set to a |zoneinfo-instance2|_, the current timezone will be mocked.
  This will be done by calling |time-tzset|_, so it is only available on Unix.

  .. |zoneinfo-instance2| replace:: ``zoneinfo.ZoneInfo`` instance
  .. _zoneinfo-instance2: https://docs.python.org/3/library/zoneinfo.html#zoneinfo.ZoneInfo

  .. |time-tzset| replace:: ``time.tzset()``
  .. _time-tzset: https://docs.python.org/3/library/time.html#time.tzset

  ``time.tzset()`` changes the ``time`` module’s `timezone constants <https://docs.python.org/3/library/time.html#timezone-constants>`__ and features that rely on those, such as ``time.localtime()``.
  It won’t affect other concepts of “the current timezone”, such as Django’s (which can be changed with its |timezone-override|_).

  .. |timezone-override| replace:: ``timezone.override()``
  .. _timezone-override: https://docs.djangoproject.com/en/stable/ref/utils/#django.utils.timezone.override

  Here’s a worked example changing the current timezone:

  .. code-block:: python

      import datetime as dt
      import time
      from zoneinfo import ZoneInfo
      import time_machine

      hill_valley_tz = ZoneInfo("America/Los_Angeles")


      @time_machine.travel(dt.datetime(2015, 10, 21, 16, 29, tzinfo=hill_valley_tz))
      def test_hoverboard_era():
          assert time.tzname == ("PST", "PDT")
          now = dt.datetime.now()
          assert (now.hour, now.minute) == (16, 29)

.. autoclass:: Coordinates

  The ``start()`` method and entry of the context manager both return a ``Coordinates`` object that corresponds to the given "trip" in time.
  This has a couple methods that can be used to travel to other times.

  .. automethod:: move_to

  ``move_to()`` moves the current time to a new destination.
  ``destination`` may be any of the types supported by ``travel``.

  ``tick`` may be set to a boolean, to change the ``tick`` flag of ``travel``.

  For example:

  .. code-block:: python

      import datetime as dt
      import time
      import time_machine

      with time_machine.travel(0, tick=False) as traveller:
          assert time.time() == 0

          traveller.move_to(234)
          assert time.time() == 234

  .. automethod:: shift

  ``shift()`` takes one argument, ``delta``, which moves the current time by the given offset.
  ``delta`` may be a ``timedelta`` or a number of seconds, which will be added to destination.
  It may be negative, in which case time will move to an earlier point.

  For example:

  .. code-block:: python

      import datetime as dt
      import time
      import time_machine

      with time_machine.travel(0, tick=False) as traveller:
          assert time.time() == 0

          traveller.shift(dt.timedelta(seconds=100))
          assert time.time() == 100

          traveller.shift(-dt.timedelta(seconds=10))
          assert time.time() == 90

Escape hatch API
================

.. autodata:: escape_hatch

The ``escape_hatch`` object provides functions to bypass time-machine, calling the real datetime functions, without any mocking.
It also provides a way to check if time-machine is currently time travelling.

These capabilities are useful in rare circumstances.
For example, if you need to authenticate with an external service during time travel, you may need the real value of ``datetime.now()``.

The functions are:

* ``escape_hatch.is_travelling() -> bool``

  Returns ``True`` if ``time_machine.travel()`` is active, ``False`` otherwise.

* ``escape_hatch.datetime.datetime.now()``

  Wraps the real ``datetime.datetime.now()``.

* ``escape_hatch.datetime.datetime.utcnow()``

  Wraps the real ``datetime.datetime.utcnow()``.

* ``escape_hatch.time.clock_gettime()``

  Wraps the real ``time.clock_gettime()``.

* ``escape_hatch.time.clock_gettime_ns()``

  Wraps the real ``time.clock_gettime_ns()``.

* ``escape_hatch.time.gmtime()``

  Wraps the real ``time.gmtime()``.

* ``escape_hatch.time.localtime()``

  Wraps the real ``time.localtime()``.

* ``escape_hatch.time.strftime()``

  Wraps the real ``time.strftime()``.

* ``escape_hatch.time.time()``

  Wraps the real ``time.time()``.

* ``escape_hatch.time.time_ns()``

  Wraps the real ``time.time_ns()``.

For example:

.. code-block:: python

    import time_machine


    with time_machine.travel(...):
        if time_machine.escape_hatch.is_travelling():
            print("We need to go back to the future!")

        real_now = time_machine.escape_hatch.datetime.datetime.now()
        external_authenticate(now=real_now)