libfaketime-c
Wraps libfaketime (github.com/wolfcw/libfaketime), the LD_PRELOAD library that intercepts time() / gettimeofday() / clock_gettime() for any binary. Covers absolute-date mode (FAKETIME='2026-12-31 23:59:00'), relative offset (FAKETIME='-1d'), advance-rate (FAKETIME='@2026-12-31 23:59:00 x5' for 5x speed), per-process scope via LD_PRELOAD, and the FAKETIME_NO_CACHE for high-resolution mocking. Use when testing C/C++/any-native-binary code that needs deterministic wall-clock time. Composes dst-transition-reference + leap-second-reference.
libfaketime-c
Overview
libfaketime is the canonical LD_PRELOAD library for faking time on Linux/macOS. Per github.com/wolfcw/libfaketime, it intercepts the libc functions time(), gettimeofday(), clock_gettime(), and similar, returning a value derived from environment variables instead of the real clock.
Works for any binary that uses libc time functions - not just C/C++. Useful for Go, Rust, Python, anything-not-statically- linked.
When to use
Authoring
Install
# Debian/Ubuntu
sudo apt install faketime
# macOS
brew install libfaketime
# From source
git clone https://github.com/wolfcw/libfaketime
cd libfaketime && make && sudo make installBasic absolute-date mode
faketime '2026-12-31 23:59:00' your_commandyour_command runs as if the wall clock is 2026-12-31 23:59:00.
Alternative via LD_PRELOAD directly:
LD_PRELOAD=/usr/local/lib/faketime/libfaketime.so.1 \
FAKETIME='2026-12-31 23:59:00' \
your_commandRelative offset
faketime '-1d' your_command # 1 day in the past
faketime '+1y' your_command # 1 year in the future
faketime '+2h30m' your_command # 2h30m aheadAdvance-rate (time-speedup / time-slowdown)
faketime -f '@2026-12-31 23:59:00 x10' your_command
# Starts at 23:59:00 on 2026-12-31, advances 10x real-timeUseful for testing schedulers, cron simulations, "long-running clock progress."
High-resolution mode
FAKETIME_NO_CACHE=1 faketime '2026-12-31 23:59:00' your_commandDisables the per-second caching libfaketime does for performance. Tests that read time hundreds of times per second see consistent behaviour.
Running
Basic test
faketime '2026-03-08 02:30:00 EDT' ./my-program
# Tests behaviour at non-existent local time (spring-forward) per
# dst-transition-referenceCron-job time-skip test
# Simulate a year in 10 minutes (1 sec real = 1.46 hrs simulated)
faketime -f '@2026-01-01 00:00:00 x5256' ./cron-runnerCombined with timezone
TZ='America/New_York' faketime '2026-03-08 02:30:00' ./my-programParsing results
libfaketime doesn't emit output itself - it transparently intercepts time syscalls. Your tests assert on the program's visible behaviour:
import subprocess
def test_cron_fires_at_midnight():
result = subprocess.run(
["faketime", "2026-12-31 23:59:30", "./cron-runner"],
capture_output=True,
text=True,
timeout=5,
)
assert "Fired at 2027-01-01 00:00:00" in result.stdoutCI integration
jobs:
time-tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v5
- run: sudo apt-get install -y faketime
- run: pytest tests/time/Anti-patterns
| Anti-pattern | Why it fails | Fix |
|---|---|---|
| Try to use libfaketime on statically-linked binaries | LD_PRELOAD has no symbols to intercept | Use a language-native fake-clock |
Forget LD_PRELOAD path | Defaults to no-op | Use faketime wrapper instead of raw LD_PRELOAD |
| Time-skip too fast | Tests can't see intermediate states | Tune x<rate> to test needs |
Spring-forward test forgets TZ | UTC-only fake time; DST tests need local zone | TZ='America/New_York' faketime ... |
Forget FAKETIME_NO_CACHE=1 for fast-polling tests | Time stalls between cache refreshes | Set explicitly |
| Use against the JVM | Some JVM time methods bypass libc | Use mockclock-jvm |
| Use against Workers / Edge / browser | LD_PRELOAD not applicable | Use language-native fakes |