Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change system time for UNIX process?

I need to test my C++ program, which uses system time. The program is large and it uses third party libraries which possibly also use system time.

I want to see how my program behaves for different dates / times.

Is it possible to change the system time only for one running process in UNIX?

Many thanks...

like image 821
Andrey Rubliov Avatar asked Jan 30 '13 10:01

Andrey Rubliov


2 Answers

Well, I found the answer by myself.

In unix shell there is an environmental variable TZ which is the timezone and it is used by all C/C++ time functions.

This variable can be manipulated to set time in current unix shell to any arbitrary time (limited to number of seconds, not milliseconds) and even change date.

Examples:

export TZ=A-02:10:20

(shifts time by 2 hours 10 minutes and 20 seconds from now forward).

export TZ=A+02:10:20

(the same shift backwards)

If you want to change the date, you can use large number of hours, for example:

export TZ=A-72:10:20

Unfortunately it does not let you change date too much, from my experiments it is up to several days back/forward. So for changing month/year this does not work.

(use 'date' command to check current date/time after setting TZ variable).

To cancel all changes use

export TZ=
like image 183
Andrey Rubliov Avatar answered Oct 15 '22 02:10

Andrey Rubliov


I guess you can use libfaketime.

like image 34
Rahul Avatar answered Oct 15 '22 04:10

Rahul