44

I'm certainly trying to achieve something weird here, but I want to fake the date locally for a shell session on GNU/Linux. I need to black-box test how a program behaves at different dates, and modifying the system-wide date can have unwanted side effects (cron jobs, messed up logs, etc).

Any ideas ?

nicoulaj
  • 1,245

5 Answers5

45

You can just use executable faketime (from ubuntu repositories sudo apt-get install faketime) by:

faketime -f "-15d" date

Or even fake time in whole shell by

faketime -f "-15d" bash -l
abonec
  • 565
31

Haven't tried this one out yet. But if this is current is looks like someone already wrote the library you can preload with libfaketime.

The basic usage is:

user@host> LD_PRELOAD=/usr/local/lib/libfaketime.so.1 FAKETIME="-15d" date
Mon Nov  8 12:01:12 CEST 2007

You can use ltrace to make sure all the time functions your application uses are covered.

Kyle Brandt
  • 85,693
7

You can set the TZ variable to an oddball value.

$ date
Tue May  4 06:24:43 CDT 2010
$ date -u
Tue May  4 11:24:47 UTC 2010
$ export TZ='CDT-3:12'
$ date
Tue May  4 14:36:53 CDT 2010
$ export TZ='CDT+5:37'
$ date
Tue May  4 05:48:00 CDT 2010
3

You might be able to preload a library that has an alternative time() implementation.

0

datefudge is an alternative to libfaketime.

It's used like so:

datefudge "2020-01-01 12:00" bash

to change the running shell to noon 1st of January 2020

Alicja Kario
  • 6,449