libfaketime.so

Library for subverting current date and time to a given application.

Preload this library to a program that calls time(2) or gettimeofday(2) functions if you need to make it think that it's not the current date but a different one.

Usage

Set one of these environment variables:

FAKE_TIME_START=<sec-since-epoch>
First call to time() or gettimeofday() will return the given <sec-since-epoch>. Subsequent calls will return a time with appropriate correction, i.e. if the second call to time() occurs five seconds after the first one, it will return <sec-since-epoch>+5.
FAKE_TIME_OFFSET=<offset_sec_from_now>
Each call to time() or gettimeofday() returns current time + <offset_sec_from_now>.

If both variables are set, then FAKE_TIME_OFFSET has a precedence.

To convert a given time to <sec-since-epoch> use:

$ date -d "2000-01-01 12:13:14" +%s
946725194

Examples

$ date
Wed Sep 24 14:41:20 CEST 2003

$ FAKE_TIME_OFFSET=-86400 LD_PRELOAD=./libfaketime.so date
Tue Sep 23 14:41:20 CEST 2003

$ FAKE_TIME_START=946725194 LD_PRELOAD=./libfaketime.so date
Sat Jan 1 12:13:14 CET 2000

Note

On ELF systems it is possible to let the library print a help when invoked directly from the command line, i.e.

$ ./libfaketime.so
 libfaketime.so - Library for subverting current time.
 [...]

For this to work you must know your runtime linker at the time of compilation. The approach used in my Makefile is to build a dummy program, extract its .interp section to a separate file and finally put the content of this file to ld-so.h header.

This was tested on different linux architectures with different names of runtime linkers, e.g. /lib/ld-linux.so.2 on i386, /lib64/ld-linux-x86-64.so.2 on AMD64, /lib/ld.so.1 on S390, /lib/lib64.so.1 on S390x, etc.

Please see the Makefile below for details on how is ld-so.h generated.

Source

The main source itself:
faketime.c (Colorized)
Makefile for successfull build:
Makefile (Colorized)
Tarball both the above files and even more...
libfaketime.tar.gz

Credits

Thanks to Andreas Thienemann for clock_gettime(3) support