更改UNIX进程的系统时间? [英] Change system time for UNIX process?

查看:153
本文介绍了更改UNIX进程的系统时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要测试我的C ++程序,它使用系统时间。
程序很大,它使用的第三方库也可能使用系统时间。



我想看看我的程序对不同的日期/时间行为。



是否可以在UNIX中为一个正在运行的进程更改系统时间?



非常感谢

解决方案

嗯,我自己找到了答案。



在unix shell中有一个环境变量TZ,它是时区,它由所有C / C ++时间函数使用。



可以操作此变量以设置当前时间unix shell到任意时间(限制秒数,而不是毫秒)甚至更改日期。



示例:

  export TZ = A-02:10:20 



  export TZ = A + 02:10: 20 

(向后相同)



如果要更改日期,可以使用大量的小时数,例如:

  export TZ = A-72:10:20 

不幸的是不要让你改变日期太多,从我的实验中可以回溯到前几天。因此,为了更改月/年,这不起作用。



(使用'date'命令查看设置TZ变量后的当前日期/时间)。



要取消所有更改,请使用

  export TZ = 


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...

解决方案

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=

这篇关于更改UNIX进程的系统时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆