Testland
Browse all skills & agents

libfaketime-c

Wraps libfaketime (github.com/wolfcw/libfaketime), the LD_PRELOAD library that fakes the clock for any binary by intercepting time() / gettimeofday() / clock_gettime(). 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 FAKETIME_NO_CACHE for high-resolution mocking. Use when you need to fake time, mock the clock, or freeze time for C/C++ or any native binary that needs deterministic wall-clock time.

Install with skills.sh (any agent)

npx skills add testland/qa --skill libfaketime-c
View source

libfaketime-c

Overview

Per github.com/wolfcw/libfaketime (opens in new window), libfaketime returns a value derived from the FAKETIME environment variable instead of the real clock. Because it hooks libc, it works for any dynamically-linked binary - Go, Rust, Python, not just C/C++.

When to use

  • Testing C/C++ code that uses libc time.
  • Testing any process where you don't control the source.
  • Integration tests where one process's perceived clock matters.
  • CI tests for time-sensitive logic without overriding system clock.

How to use

  1. Install faketime on the test host (apt / brew / from source).
  2. Pick a mode: absolute date, relative offset (-1d, +1y), or advance-rate (@... x<rate>).
  3. Wrap the target binary: faketime '<spec>' your_command, or set LD_PRELOAD plus FAKETIME directly.
  4. Prefix TZ='<zone>' when the behaviour depends on a local zone, for example DST transitions.
  5. Set FAKETIME_NO_CACHE=1 when the code reads time many times per second.
  6. Assert on the program's visible output or behaviour from your test runner (libfaketime emits nothing itself).
  7. Install faketime in CI before running the time-sensitive suite.

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 install

Basic absolute-date mode

faketime '2026-12-31 23:59:00' your_command

your_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_command

Relative offset (-1d, +1y), advance-rate (-f '@... x<rate>' for time speed-up), high-resolution mode (FAKETIME_NO_CACHE=1), and the spring-forward / cron time-skip / timezone recipes are in references/faketime-modes-and-recipes.md.

Parsing 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.stdout

Worked example

Verify a scheduler's behaviour at the non-existent local time 02:30 on a spring-forward day, without changing the host clock:

  1. Pick the transition: US Eastern springs forward at 02:00 on 2026-03-08, so 02:30 local does not exist that day.
  2. Run with both the zone and the fake instant: TZ='America/New_York' faketime '2026-03-08 02:30:00' ./my-program.
  3. libfaketime returns that wall-clock instant from libc, and TZ makes the program resolve it in Eastern.
  4. Assert the program either skips the job or normalises to 03:30 (whichever the spec requires), matching dst-transition-reference.

Result: the spring-forward branch runs deterministically on every CI run, with no dependence on the real date or the host clock.

CI 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-patternWhy it failsFix
Try to use libfaketime on statically-linked binariesLD_PRELOAD has no symbols to interceptUse a language-native fake-clock
Forget LD_PRELOAD pathDefaults to no-opUse faketime wrapper instead of raw LD_PRELOAD
Time-skip too fastTests can't see intermediate statesTune x<rate> to test needs
Spring-forward test forgets TZUTC-only fake time; DST tests need local zoneTZ='America/New_York' faketime ...
Forget FAKETIME_NO_CACHE=1 for fast-polling testsTime stalls between cache refreshesSet explicitly
Use against the JVMSome JVM time methods bypass libcUse mockclock-jvm
Use against Workers / Edge / browserLD_PRELOAD not applicableUse language-native fakes

Limitations

  • Linux + macOS only. Windows uses different time syscalls.
  • Statically linked binaries unaffected. Go binaries compiled with CGO_ENABLED=0 don't see libfaketime.
  • JVM bypasses libc for some time calls (Instant from monotonic system clock). Use mockclock-jvm.
  • Doesn't fake monotonic clocks by default; some clock_gettime flags pass through.
  • No leap-second simulation. See leap-second-reference.

References

  • libfaketime: github.com/wolfcw/libfaketime (opens in new window).
  • Companion catalogs: dst-transition-reference, leap-second-reference, iso-8601-vs-rfc-3339-reference.
  • Language-native siblings: sinon-fake-timers-js, jest-fake-timers, freezegun-python, timecop-ruby, mockclock-jvm.
  • Test matrix: timezone-test-matrix-builder.

libfaketime modes and recipes

View source (opens in new window)

libfaketime modes and recipes

Deeper variants beyond the core absolute-date mode. All flags and syntax per github.com/wolfcw/libfaketime (opens in new window).

Relative offset

Move the clock a fixed delta from real time instead of pinning an absolute instant:

faketime '-1d' your_command         # 1 day in the past
faketime '+1y' your_command         # 1 year in the future
faketime '+2h30m' your_command      # 2h30m ahead

Advance-rate (time speed-up / slow-down)

The -f flag plus an @start xRATE spec starts at a fixed instant and advances faster (or slower) than real time:

faketime -f '@2026-12-31 23:59:00 x10' your_command
# Starts at 23:59:00 on 2026-12-31, advances 10x real-time

Useful for testing schedulers, cron simulations, and long-running clock progress.

High-resolution mode

FAKETIME_NO_CACHE=1 faketime '2026-12-31 23:59:00' your_command

Disables the per-second caching libfaketime does for performance. Tests that read time hundreds of times per second then see consistent behaviour.

Recipes

Spring-forward (non-existent local time)

faketime '2026-03-08 02:30:00 EDT' ./my-program
# Tests behaviour at a non-existent local time (spring-forward) per
# dst-transition-reference

Cron-job time-skip

# Simulate a year in ~10 minutes (1 sec real = 1.46 hrs simulated)
faketime -f '@2026-01-01 00:00:00 x5256' ./cron-runner

Combined with a timezone

TZ='America/New_York' faketime '2026-03-08 02:30:00' ./my-program

Related skills

dotnet-faketime

Wraps .NET's TimeProvider abstraction (System.TimeProvider, introduced .NET 8) and FakeTimeProvider from Microsoft.Extensions.TimeProvider.Testing: SetUtcNow, Advance, AutoAdvanceAmount, CreateTimer, Delay, and the pre-.NET-8 ISystemClock migration path. Use when testing C# or F# code that reads the current time, uses timers, or awaits Task.Delay.

dst-transition-reference

Pure-reference catalog of Daylight Saving Time (DST) transition patterns and their canonical bug classes. Covers the spring-forward (skipped hour: 02:00 → 03:00 local) and fall-back (repeated hour: 02:00 → 01:00 local) transitions, the historical irregularity of DST (different jurisdictions, transitions on different dates, some regions abolish DST or never adopted it), the IANA timezone database (tz / Olson DB) as the canonical source, and the testable behaviors DST creates (duplicate / missing local timestamps, cron jobs that fire 0 or 2 times, billing periods that miss / double-count, recurring meetings on transition days). Per-jurisdiction DST-rule tables and refreshable per-region test-data fixtures live in references/. Use when designing or auditing time-handling code or test cases.

freezegun-python

Wraps freezegun (github.com/spulec/freezegun), the Python time-mocking library: @freeze_time decorator / context manager, freeze_time(...).start() + stop(), tick / move_to / tz_offset, and integration with datetime.now / time.time / time.localtime. Use when testing Python code that calls datetime / time.

iso-8601-vs-rfc-3339-reference

Pure-reference catalog of the ISO 8601 vs RFC 3339 distinction. Covers the relationship (RFC 3339 is a strict subset of ISO 8601 designed for internet protocols), the syntactic differences (RFC 3339 disallows ISO 8601's '+02' offset short-form requires '+02:00'; RFC 3339 mandates a date-time separator T or space; ISO 8601 allows much more), the canonical date-time string format (YYYY-MM-DDTHH:MM:SS[.fff]±HH:MM or Z), per-language parser behaviour (Python isoformat, Java Instant.parse, JS Date.parse non-spec), and serialisation rules for APIs. Use when choosing a wire format, parsing third-party datetimes, or auditing time-string handling.

jest-fake-timers

Wraps Jest's built-in modern fake-timers (built on @sinonjs/fake-timers since Jest 27): jest.useFakeTimers(), jest.setSystemTime(), jest.advanceTimersByTime(), jest.runAllTimers(), and jest.useRealTimers() for selective restoration. Use when testing JS/TS code in Jest where setTimeout / setInterval / Date / Date.now need deterministic control.

leap-second-reference

Pure-reference catalog of leap-second mechanics and the bugs they cause: the 23:59:60 UTC insertion (announced ~6 months ahead by IERS Bulletin C; 27 inserted 1972-2016; abolished by 2035 per CGPM 2022), the Google/AWS leap-smear alternative, and the four bug classes a real insertion exposes - time_t non-monotonicity, negative durations, NTP cascading, and cross-node clock skew - each with a monotonic-clock fix and a freezegun simulation. Use when auditing time-sensitive code (financial timestamping, distributed logs, NTP-driven schedulers) for second-by-second progress assumptions; for the far more common daylight-saving-time transition hazards, use dst-transition-reference instead.

mockclock-jvm

Wraps Java's java.time.Clock + InstantSource dependency-injection pattern for testing time-sensitive code. Covers Clock.fixed(instant, zone), Clock.offset(baseClock, duration), Clock.systemDefaultZone() for production, the InstantSource interface (Java 17+), and the recommended dependency-injection pattern (constructor-inject Clock instead of calling Instant.now() directly). Use when you need to wire the clock-injection pattern (Clock.fixed, Clock.offset, MutableClock, InstantSource, Spring @Bean) into JVM (Java / Kotlin / Scala) production or test code. For pure DST transition reference (skipped or repeated hours, IANA DB, cron-double-fire bug classes) without a clock-injection need, use dst-transition-reference instead.

sinon-fake-timers-js

Wraps Sinon's standalone @sinonjs/fake-timers library for JS/TS testing: install(), tick() / tickAsync(), setSystemTime(), restore(); covers timers (setTimeout / setInterval / requestAnimationFrame), Date / performance.now() / hrtime, and the toFake option for selective override. Runner-agnostic - drives the clock directly in Mocha, AVA, Jasmine, node:test, or the browser. Use when JS/TS code needs deterministic timer or clock control and the test runner does not already expose this library behind its own built-in fake-timer API.

timecop-ruby

Wraps timecop (github.com/travisjeffery/timecop), the Ruby time-mocking gem: Timecop.freeze, Timecop.travel, Timecop.scale (time-speedup), Timecop.return (cleanup), and RSpec-friendly helpers. Use when testing Ruby/Rails code that calls Time / Date / DateTime.

timezone-test-matrix-builder

Builds a timezone, daylight saving time (DST), and leap year / leap second test matrix from wherever a codebase reads or formats dates and times. Finds time-handling code (grep for datetime / Date / Instant / time.time / timezone), sorts each spot into storage, business-logic, display, cron, or billing, picks the edge cases that matter (DST spring-forward / fall-back, ambiguous local time, leap day Feb 29, ISO 8601 / RFC 3339 round-trip, zone-database updates), and emits per-spot test stubs wired to the language's fake-clock (mock-time) library. Use when a codebase needs timezone, DST, and leap-year test coverage derived from its own date/time usage.