在Linux上使用时间偏移调用程序

我想在Linux上调用具有相对时间偏移量(或者绝对时间值)的程序或脚本。 可以把它看作设置系统的date和时间,除了系统不是全局的,而是脚本/程序及其subprocess在本地。 那可能吗?

你可以创建一个围绕C运行时库(glibc)调用的包装,并用LD_PRELOAD传递给你的脚本(更多信息见这个问题 )。

没有太多的函数需要重写, LSB在glibc中只指定了16个与时间相关的函数。 您的包装的实现可以使用环境变量的值来调整偏斜。 电话会看起来像这样:

 LD_PRELOAD=libtime.so TIME_SHIFT=+10 my_program 

实现将如下所示(伪代码):

 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 } 

感谢Konstantin Vlasov的提示。 但是,这个想法当然不是新颖的。 这个由Hasturkun的 StackOverflow答案列出了几个库,这样的工作。