Build with frame pointers enabled, preparation for PEP 831.
Stop shipping wheels for 32-bit Linux and Windows.
Add :attr:`time_machine.naive_mode` to control how time-machine interprets naive datetimes.
The default mode is
MIXED, which preserves existing behaviour: naivedatetimeobjects anddateobjects are interpreted as UTC, while naive datetime strings are interpreted as local time.Three alternative modes are available:
UTC: naive datetimes are always interpreted as UTC.LOCAL: naive datetimes are interpreted as local time, matching Python's default semantics, and freezegun.ERROR: naive datetimes raise aRuntimeError, ensuring your tests are isolated from the current timezone.
Note
It’s recommended you use
LOCALorERRORto avoid confusion around naive datetimes.PR #591. Thanks to Paolo Melchiorre for review.
Thanks to PhML, Stefaan Lippens, Matthieu Rigal, Nikita Demir, Steve Mavens, Andy Freeland, and Paul Ganssle for their input on Issue #257.
Raise
RuntimeErrorwhen attempting to start time travelling if freezegun is active.This change should help avoid surprises when migrating complex test suites from freezegun to time-machine.
Optimize patching of
uuidmodule. By avoiding usingunittest.mock, this small overhead from startingtime_machine.travel()has been reduced about 20x, from ~600ns to ~30ns by one benchmark.
Remove mocking of
time.monotonic()andtime.monotonic_ns().This mocking caused too many issues, such as causing freezes in asyncio event loops (Issue #387), preventing pytest-durations from timing tests correctly (Issue #505), and triggering timeouts in psycopg (Issue #509). The root cause here is that mocking the monotonic clock breaks its contract, allowing it to move backwards when it’s meant to only move forwards.
As an alternative, use
unittest.mockto mock the monotonic function for the specific tested modules that need it. That means that your code should importmonotonic()ormonotonic_ns()directly, so that your tests can mock it in those places only. For example, if your system under test looks like:# example.py from time import monotonic def measurement(): start = monotonic() ... end = monotonic() return end - start
…then your tests can mock
monotonic()like this:from unittest import TestCase, mock import example class MeasurementTests(TestCase): def test_success(self): with mock.patch.object(example, "monotonic", side_effect=[0.0, 1.23]): result = example.measurement() assert result == 1.23
Parse
strdestinations withdatetime.fromisoformat()first, before falling back to dateutil if installed.datetime.fromisoformat()can parse most valid ISO 8601 formats, with better performance and no extra dependencies.Make the dependency on dateutil optional. To include dateutil support, install with the
dateutilextra:python -m pip install time-machine[dateutil]
Beware that some of the formats that dateutil parses are ambiguous and may lead to unexpected results.
Rename the
Coordinatesclass toTraveller, to match the recommended context manager variable name.Thanks to Matt Wang in PR #535.
Drop Python 3.9 support.
Make the
escape_hatchfunctions raiseValueErrorwhen called outside of time-travelling, rather than triggering segmentation faults.
Add marker support to :doc:`the pytest plugin <pytest_plugin>`. Decorate tests with
@pytest.mark.time_machine(<destination>)to set time during a test, affecting function-level fixtures as well.Thanks to Javier Buzzi in PR #499.
Add asynchronous context manager support to
time_machine.travel(). You can now useasync with time_machine.travel(...):in asynchronous code, per :ref:`the documentation <travel-context-manager>`.Import date and time functions once in the C extension.
This should improve speed a little bit, and avoid segmentation faults when the functions have been swapped out, such as when freezegun is in effect. (time-machine still won’t apply if freezegun is in effect.)
Update the :ref:`migration CLI <migration-cli>` to detect unittest classes based on whether they use
self.assert*methods likeself.assertEqual().Fix free-threaded Python warning:
RuntimeWarning: The global interpreter lock (GIL) has been enabled...as seen on Python 3.13+.Thanks to Javier Buzzi in PR #531.
Add support to
travel()fordatetimedestinations withtzinfoset todatetime.UTC(datetime.timezone.utc).Thanks to Lawrence Law in PR #502.
Prevent segmentation faults in unlikely scenarios, such as if the
time_machinemodule cannot be imported.Make
travel()fully unpatch date and time functions when travel ends. This may fix certain edge cases.
Include wheels for Python 3.14.
Thanks to Edgar Ramírez Mondragón in PR #521.
Support free-threaded Python.
Thanks to Javier Buzzi in PR #500.
Add a new CLI for migrating code from freezegun to time-machine.
Install with
pip install time-machine[cli]and run withpython -m time_machine migrate.Move the documentation to Read the Docs, and add a retro-futuristic logo.
- Drop Python 3.8 support.
- Include wheels for Python 3.13.
Fix
SystemErroron Python 3.13 and Windows when starting time travelling.Thanks to Bernát Gábor for the report in Issue #456.
Fix segmentation fault when the first
travel()call in a process uses atimedelta.Thanks to Marcin Sulikowski for the report in Issue #431.
Fix
utcfromtimestamp()warning on Python 3.12+.Thanks to Konstantin Baikov in PR #424.
Fix class decorator for classmethod overrides.
Thanks to Pavel Bitiukov for the reproducer in PR #404.
Avoid calling deprecated
uuid._load_system_functions()on Python 3.9+.Thanks to Nikita Sobolev for the ping in CPython Issue #113308.
Support Python 3.13 alpha 4.
Thanks to Miro Hrončok in PR #409.
Add support for
datetime.timedeltatotime_machine.travel().Thanks to Nate Dudenhoeffer in PR #298.
Fix documentation about using local time for naive date(time) strings.
Thanks to Stefaan Lippens in PR #306.
Add
shift()method to thetime_machinepytest fixture.Thanks to Stefaan Lippens in PR #312.
Mock
time.monotonic()andtime.monotonic_ns(). They return the values oftime.time()andtime.time_ns()respectively, rather than real monotonic clocks.Thanks to Anthony Sottile in PR #382.
- Include wheels for Python 3.12.
- Drop Python 3.7 support.
- Support Python 3.12.
- Build Windows ARM64 wheels.
- Explicitly error when attempting to install on PyPy.
Thanks to Michał Górny in PR #315.
- Improve type hints for
time_machine.travel()to preserve the types of the wrapped function/coroutine/class.
- Actually build Python 3.11 wheels.
- Build Python 3.11 wheels.
- Fix usage of
ZoneInfofrom thebackports.zoneinfopackage. This makesZoneInfosupport work for Python < 3.9.
- Support Python 3.11 (no wheels yet, they will only be available when Python 3.11 is RC when the ABI is stable).
- Drop Python 3.6 support.
Add
time_machine.escape_hatch, which provides functions to bypass time-machine.Thanks to Matt Pegler for the feature request in Issue #206.
- Build musllinux wheels.
- Support Python 3.10.
- Build universal2 wheels for Python 3.8 on macOS.
- Allow passing
ticktoCoordinates.move_to()and the pytest fixture’stime_machine.move_to(). This allows freezing or unfreezing of time when travelling.
- Include type hints.
- Convert C module to use PEP 489 multi-phase extension module initialization. This makes the module ready for Python sub-interpreters.
- Release now includes a universal2 wheel for Python 3.9 on macOS, to work on Apple Silicon.
- Stop distributing tests to reduce package size. Tests are not intended to be run outside of the tox setup in the repository. Repackagers can use GitHub's tarballs per tag.
- Release now includes wheels for ARM on Linux.
- Prevent
ImportErroron Windows wheretime.tzset()is unavailable.
- Release now includes wheels for Windows and macOS.
- Move internal calculations to use nanoseconds, avoiding a loss of precision.
- After a call to
move_to(), the first function call to retrieve the current time will return exactly the destination time, copying the behaviour of the first call totravel(). - Add the ability to shift timezone by passing in a
ZoneInfotimezone. - Remove
tz_offsetargument. This was incorrectly copied fromfreezegun. Use the new timezone mocking withZoneInfoinstead. - Add pytest plugin and fixture
time_machine. - Work with Windows’ different epoch.
- Support Python 3.9.
- Move license from ISC to MIT License.
Correctly return naive datetimes from
datetime.utcnow()whilst time travelling.Thanks to Søren Pilgård and Bart Van Loon for the report in Issue #52.
- Add
move_to()method to move to a different time whilst travelling. This is based on freezegun'smove_to()method.
- Move C-level
clock_gettime()andclock_gettime_ns()checks to runtime to allow distribution of macOS wheels.
Add
shift()method to move forward in time by a delta whilst travelling. This is based on freezegun'stick()method.Thanks to Alex Subbotin for the feature in PR #27.
Fix to work when either
clock_gettime()orCLOCK_REALTIMEis not present. This happens on some Unix platforms, for example on macOS with the official Python.org installer, which is compiled against macOS 10.9.Thanks to Daniel Crowe for the fix in PR #30.
- Fix
datetime.now()behaviour with thetzargument when not time-travelling.
- First non-beta release.
- Added support for
tz_offsetargument. tick=Truewill only start time ticking after the first method return that retrieves the current time.- Added nestability of
travel(). - Support for
time.time_ns()andtime.clock_gettime_ns().
- First release on PyPI.