Skip to main content

Faketime

Faketime is a thin python wrapper around the amazing C library libfaketime, written by Wolfgang Hommel which you can use to lie to UNIX processes about what time and date it is.

Why does this wrapper exist?

  • It provides an interface that is convenient and pythonic that you can use to lie to subprocesses run with python.
  • It provides an python interface that is identical on both Mac OS X and Linux (libfaketime is used in a slightly different way on both).
  • It provides a self contained library that can be installed in a virtualenv that runs consistently in any environment - installing libfaketime from package manager can instead get you older, buggy versions on different operating systems and package managers.

Install

pip install faketime

Using

>>> from commandlib import Command
>>> from faketime import Faketime
>>> from datetime import datetime
>>> faketime = Faketime("currenttime.txt")
>>> datecmd
{'LD_PRELOAD': '/full/path/to/virtualenv/site-packages/faketime/libfaketime.so.1', 'FAKETIME_TIMESTAMP_FILE': '/full/path/to/currenttime.txt'}


>>> datecmd = Command("date").with_env(**faketime.env_vars)
>>> datecmd.run()
[ should print current time ]

>>> faketime.change_time(datetime(2050, 6, 7, 10, 9, 22, 713689))
>>> datecmd.run()
Tue  7 Jun 10:09:21 BST 2050

This above example shows how to use faketime with commandlib, although the environment variables in the dict can be used with any command runner (e.g. Popen).

What's the story behind this library?

This library can be used for a number of purposes, but I mainly built it so that I could write tests with hitchstory that would lie to postgres, django and celery simultaneously about what date and time it was.

Initially I tried using freezegun to test the python code, but I realized that it only really worked on one snippet of code at a time. Moreover:

  • If that code executed a python process that contained other code that other code would get the current time, not the frozen time, breaking the test.
  • It was simply incapable of faking, say, postgres's time, so if an SQL query embedded a datetime query then that would break the test.

Metadata

Release files for faketime 0.9.8

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for faketime 0.9.8
File Size Uploaded
faketime-0.9.8.tar.gz 41.8 kB Details

Release files / faketime-0.9.8.tar.gz

Download URL faketime-0.9.8.tar.gz
Size 41.8 kB
Tags Source
SHA-256 checksum
How to use checksums
b12e43b355489b1938ed2b1db4ad2f2c83df5b2bd8c412a026965628669a2160
BLAKE2b-256 checksum
How to use checksums
2a4f14ae990f996f14c6b229938d1713a2ae067d3added78a2610c6baa65472e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.3

Release history Release notifications | RSS feed

This release

0.9.8 This release

1 release file

0.9.6.9

1 release file

0.9.6.8

1 release file

0.9.6.7

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page