DEV.co
Open-Source Testing · adamchainz

time-machine

time-machine is a Python testing library that lets developers mock or freeze time during unit tests. It intercepts time-related system calls to simulate traveling to any point in the past or future, making it easier to test time-dependent code without waiting or using the actual system clock.

Source: GitHub — github.com/adamchainz/time-machine
979
GitHub stars
48
Forks
Python
Primary language
MIT
License (OSI-approved)

Key facts

Objective fields from the source. Values we can't verify are shown as “Unknown” rather than guessed.

FieldValue
Repositoryadamchainz/time-machine
Owneradamchainz
Primary languagePython
LicenseMIT — OSI-approved
Stars979
Forks48
Open issues25
Latest releaseUnknown
Last updated2026-07-06
Sourcehttps://github.com/adamchainz/time-machine

What time-machine is

A Python package that patches time-related builtins and standard library functions (datetime, time, etc.) to allow tests to control the current time state. It supports context managers and decorators for granular time manipulation within test scopes, with 100% test coverage reported.

Quickstart

Get the time-machine source

Clone the repository and explore it locally.

terminalbash
git clone https://github.com/adamchainz/time-machine.gitcd time-machine# follow the project's README for install & configuration

Need it deployed, integrated, or customized instead? DEV.co ships production installs.

Best use cases

Testing deadline-based logic

Verify code that checks expiration dates, time windows, or rate limits by freezing or advancing time without waiting real seconds.

Testing scheduled task behavior

Validate cron jobs, task queues, or delayed execution by simulating future timestamps without requiring external time infrastructure.

Testing timezone and DST edge cases

Reliably reproduce timezone-sensitive behavior, daylight saving transitions, and date arithmetic across different locales in isolation.

Implementation considerations

  • Ensure all time-dependent code uses standard Python datetime/time modules; third-party or custom time implementations may not be patched.
  • Test isolation: time state persists across test methods unless explicitly reset; use context managers or decorators to scope changes.
  • Verify behavior with libraries that cache datetime objects at import time (e.g., logging formatters, cache headers) as they may see stale values.
  • Confirm compatibility with your Python version and test runner (pytest, unittest, etc.) before adoption.
  • Document mocking strategy in test setup to make time freezing explicit to future maintainers.

When to avoid it — and what to weigh

  • Testing async/concurrent time behavior — Project documentation does not explicitly confirm thread-safe or async-compatible time patching; concurrency bugs may not be caught.
  • Integration tests requiring real system clock — If tests need to interact with external services that validate wall-clock time server-side, mocking client-side time may cause failures.
  • Testing C extensions or native code directly — time-machine patches Python-level time calls; compiled code or system calls that bypass Python bindings will not be affected.
  • Projects requiring clock adjustment during test execution — No evidence of support for incrementally advancing time in sub-second intervals; only snapshot/freeze-then-jump workflows are documented.

License & commercial use

MIT License permits unrestricted use, modification, and distribution in proprietary and open-source projects, provided copyright and license text are retained.

MIT is a permissive OSI-approved license with no commercial restrictions. Use in proprietary products is permitted; however, ensure your legal team reviews license attribution requirements for your distribution method.

DEV.co evaluation signals

Editorial assessment — not user reviews. Directional, with an explicit confidence level.

SignalAssessment
MaintenanceActive
DocumentationStrong
License clarityClear
Deployment complexityLow
DEV.co fitGood
Assessment confidenceHigh
Security considerations

time-machine patches Python's time representation at the interpreter level during tests. Review implications: (1) ensure test and production code paths are clearly separated to avoid mocked time in production, (2) validate no sensitive time-based security logic (e.g., token expiry checks) relies on patched time in unexpected contexts, (3) confirm usage does not mask real timing bugs (e.g., performance regressions) that integration tests should catch.

Alternatives to consider

freezegun

Popular alternative with similar scope (freeze/mock time); may have broader decorator/context support or different patching strategy. Compare if time-machine doesn't cover your use case.

pytest-freezegun

Pytest-specific plugin wrapping freezegun; tighter integration with pytest fixtures if that's your test runner.

unittest.mock with patch

Standard library approach; less specialized but requires manual patching of every time call. Useful if you want zero external dependencies.

Software development agency

Build on time-machine with DEV.co software developers

Adopt time-machine to eliminate flaky tests, reduce test execution time, and reliably reproduce timezone and deadline scenarios. Review the documentation and integrate it into your test suite today.

Talk to DEV.co

Related open-source tools

Surfaced by semantic similarity across the DEV.co open-source index.

Related on DEV.co

Explore the category and the services that help you build with it.

time-machine FAQ

Does time-machine work with async code?
Not clearly documented in the provided README. Requires review of full documentation or testing before adopting in async-heavy codebases.
Can I run multiple time-travel operations in parallel test runs?
Unknown; no mention of thread-safety or multiprocessing isolation. Integration with concurrent test runners (pytest-xdist, etc.) requires verification.
Will mocked time affect external services or databases?
Only Python-side time calls are patched. External services see no change; however, your code's queries or API payloads will use the mocked timestamp if they're built in Python.
What Python versions are supported?
Not stated in README excerpt. Check PyPI or GitHub releases page for supported version range.

Custom software development services

Need help beyond evaluating time-machine? DEV.co is a software development agency offering software development services and web development for teams of every size. Our software developers and web developers build custom software, web applications, APIs, and open-source testing integrations — and maintain them long-term.

Ready to simplify time-dependent testing?

Adopt time-machine to eliminate flaky tests, reduce test execution time, and reliably reproduce timezone and deadline scenarios. Review the documentation and integrate it into your test suite today.