views:

39

answers:

1

I would like to call a program or a script with a relative time offset (or, alternatively, with an absolute time value) on Linux. Think of it as setting the system date and time, except not globally for the system but locally for the script/program and all its child processes. Is that possible?

+2  A: 

You can create a wrapper around C runtime library (glibc) calls and pass it to your script with LD_PRELOAD (see this question for more info).

There are not so many functions you are to override, LSB specifies only 16 time-related functions in glibc. The implementation of your wrappers could use a value of an environment variable to adjust the skew. The call would then look like this:

LD_PRELOAD=libtime.so TIME_SHIFT=+10 my_program

And the implementation would look like this (pseudocode):

struct tm *localtime(const time_t *timep)
{
  //use dlopen() call to get the actual glibc
  //use dlsym() to find the real localtime() function
  //call this localtime function
  //adjust the time in the struct tm* returned by TIME_SHIFT value
  //return it to the calling program
}

Thanks to Konstantin Vlasov for the hint. But of course the idea is not novel. This StackOverflow answer by Hasturkun lists several libraries, which work this way.

Pavel Shved
There are libraries to do this sort of thing, see http://stackoverflow.com/questions/3625922/linux-how-to-set-up-a-timezone-of-a-process/3626053#3626053 for a couple of those
Hasturkun
@Hasturkun, thanks for the information, merged into the answer.
Pavel Shved