Package: libfaketime
python-libfaketime is a wrapper oflibfaketimefor python.
Some brief details:Linux and OS X, Pythons 3.5 through 3.8, pypy and pypy3Mostly compatible withfreezegun.Microsecond resolution.Accepts datetimes and strings that can be parsed by dateutil.Not threadsafe.Will break profiling. A workaround: uselibfaketime.{begin,end}_callbackto disable/enable your profiler (nosetest example).Installation$pipinstalllibfaketimeUsageimportdatetimefromlibfaketimeimportfake_time,reexec_if_needed# libfaketime needs to be preloaded by the dynamic linker.# This will exec the same command, but with the proper environment variables set.# You can also skip this and manually manage your env (see "How to avoid re-exec").reexec_if_needed()deftest_datetime_now():# fake_time can be used as a context_managerwithfake_time('1970-01-01 00:00:01'):# Every calls to a date or datetime function returns the mocked dateassertdatetime.datetime.utcnow()==datetime.datetime(1970,1,1,0,0,1)assertdatetime.datetime.now()==datetime.datetime(1970,1,1,0,0,1)asserttime.time()==1# fake_time can also be used as a decorator@fake_time('1970-01-01 00:00:01',tz_offset=12)deftest_datetime_now_with_offset():# datetime.utcnow returns the mocked datetime without offsetassertdatetime.datetime.utcnow()==datetime.datetime(1970,1,1,0,0,1)# datetime.now returns the mocked datetime with the offset passed to fake_timeassertdatetime.datetime.now()==datetime.datetime(1970,1,1,12,0,1)Performancelibfaketime tends to be significantly faster thanfreezegun.
Here’s the output of atotally unscientific benchmarkon my laptop:$pythonbenchmark.pyre-execwithlibfaketimedependenciestiming1000executionsof<class'libfaketime.fake_time'>0.021755seconds$pythonbenchmark.pyfreezeguntiming1000executionsof<functionfreeze_timeat0x10aaa1140>6.561472secondsUse with py.testThepytest-libfaketimeplugin will automatically configure python-libfaketime for you:$pipinstallpytest-libfaketimeAlternatively, you can reexec manually from inside the pytest_configure hook:# conftest.pyimportosimportlibfaketimedefpytest_configure():libfaketime.reexec_if_needed()_,env_additions=libfaketime.get_reload_information()os.environ.update(env_additions)Use with toxIn your tox configuration file, under thetestenvbloc, add the libfaketime environment variables to avoid re-execution:setenv =
    LD_PRELOAD = {envsitepackagesdir}/libfaketime/vendor/libfaketime/src/libfaketime.so.1
    DONT_FAKE_MONOTONIC = 1
    FAKETIME_DID_REEXEC = trueMigration from freezegunpython-libfaketime should have the same behavior as freezegun when running on supported code. To migrate to it, you can run:find.-typef-name"*.py"-execsed-i's/freezegun/libfaketime/g'"{}"\;How to avoid re-execIn some cases - especially when your tests start other processes - re-execing can cause unexpected problems. To avoid this, you can preload the necessary environment variables yourself. The necessary environment for your system can be found by runningpython-libfaketimeon the command line:$python-libfaketimeexportLD_PRELOAD="/home/foo/<snip>/vendor/libfaketime/src/libfaketime.so.1"exportDONT_FAKE_MONOTONIC="1"exportFAKETIME_DID_REEXEC=trueYou can easily put this in a script like:$eval$(python-libfaketime)$pytest# ...or any other code that imports libfaketimeContributing and testingContributions are welcome! You should compile libfaketime before running tests:make-Clibfaketime/vendor/libfaketimeThen you can install requirements withpip install-rrequirements.txtand usepytestandtoxto run the tests.uuid1 deadlockCallinguuid.uuid1()multiple times while in a fake_time context can result in a deadlock when an OS-level uuid library is available.
To avoid this, python-libtaketime will monkeypatch uuid._uuid_generate_time (or similar, it varies by version) to None inside a fake_time context.
This may slow down uuid1 generation but should not affect correctness.ChangelogSemantic versioningis used.2.0.0released 2020-04-17breaking: drop python 2.7 supportset LD_LIBRARY_PATH on linux to support paths containing spaces:#57fix compatibility with non-pytz tzinfo objects:#581.2.1released 2019-01-20fix a deadlock on python 3.7+1.2.0released 2018-10-28offset-aware datetimes now properly fake the timezone as well:#491.1.0released 2018-10-07decorated classes can access the fake_time object withself._faked_time:#471.0.0released 2018-06-16backwards incompatible: the monotonic clock is no longer mocked:#45ensure TZ is set to a valid timezone:#460.5.2released 2018-05-19fix a bug causing incorrect times after unpatching under python 3.6+:#43fix compilation under gcc8:#440.5.1released 2018-01-19fix usage as a class decorator :#410.5.0released 2017-09-10alias fake_time for freeze_time:#31add tz_offset parameter:#360.4.4released 2017-07-16allow contextlib2 as an alternative to contextdecorator:#300.4.3released 2017-07-07add macOS Sierra compatibility:#290.4.2released 2016-06-30fix only_main_thread=False:#240.4.1released 2016-05-02fix deadlocks from uuid.uuid1 when faking time:#14remove contextdecorator dependency on python3:#150.4.0released 2016-04-02freezegun’s tick() is now supported; seetheir docsfor usage.0.3.0released 2016-03-04invokinglibfaketimefrom the command line will now print the necessary environment to avoid a re-exec.0.2.1released 2016-03-01python 3 support0.1.1released 2015-09-11prevent distribution of test directory:https://github.com/simon-weber/python-libfaketime/pull/40.1.0released 2015-06-23add global start/stop callbacks0.0.3released 2015-03-28initial packaged release