Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Faking date/time of child process

Tags:

unix

posix

On Unix systems, is there a way to fake the perceived date and time of a child process?

I.e., imagine:

$ date 
Fri Jun 28 10:50:35 CEST 2019

$ with_date 10/05/2019 date
Fri May 10 10:50:36 CEST 2019

How to implement the with_date command?

The typical use case would be the testing of date/time-related software, simulating various conditions.

like image 997
gigabytes Avatar asked Jun 28 '19 08:06

gigabytes


1 Answers

There is the library libfaketime. It uses a library preload mechanism to intercept system calls of the to-be-run programs. A use-case (from the manual) is:

user@host> date
Tue Nov 23 12:01:05 CEST 2016

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

user@host> LD_PRELOAD=/usr/local/lib/libfaketime.so.1 FAKETIME="-15d" FAKETIME_DONT_FAKE_MONOTONIC=1 java -version
java version "1.8.0_111"
Java(TM) SE Runtime Environment (build 1.8.0_111-b14) Java HotSpot(TM) 64-Bit Server VM (build 25.111-b14, mixed mode)

like image 91
Andreas H. Avatar answered Sep 24 '22 08:09

Andreas H.