Windows程序如何临时更改其时区? [英] How can a Windows program temporarily change its time zone?

查看:124
本文介绍了Windows程序如何临时更改其时区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个函数来返回对应于给定日期的午夜的 time_t 值。当给定日期没有午夜时,它返回可用的最早时间;这种情况可能发生,例如,当埃及进入夏季时间时。今年,时间变化在4月29日晚上的午夜生效,所以时钟直接从23:59到01:00。

I've written a function to return the time_t value corresponding to midnight on a given day. When there is no midnight for a given day, it returns the earliest time available; that situation can occur, for example, when Egypt enters daylight-saving time. This year, the time change takes effect at midnight on the night of April 29, so the clock goes directly from 23:59 to 01:00.

现在我写单元测试这个功能,其中一个测试应该复制埃及场景。在Unix中,我可以这样完成它:

Now I'm writing unit tests for this function, and one of the tests should replicate the Egypt scenario. In Unix, I can accomplish it like this:

putenv("TZ", "Egypt", true);
tzset();



<表现得好像他们在埃及而不是明尼苏达州,我的测试通过了。只是设置环境变量对Windows没有任何影响。

After doing that, further calls to localtime behave as if they're in Egypt instead of Minnesota, and my tests pass. Merely setting the environment variable doesn't have any effect on Windows, though. What can I do to make the unit test think it's somewhere else without affecting the rest of the programs running on the system?

推荐答案

我可以做什么让单元测试认为它在别的地方,而不影响系统上运行的其余程序。使用隔离/模拟框架 - 我目前唯一知道的是 Isolator ++ 这是目前在测试版,我相信你可以得到一个副本,从一个来自Typemock的好人。

Use Isolation/Mocking framework - the only one I know of at the moment is Isolator++ which is currently in beta, I'm sure that you can get a copy by asking for one from the good people of Typemock.

这篇关于Windows程序如何临时更改其时区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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