开发者

SystemEvent.TimeChange showing same time even timezone changes

I have wrote this simple console app to test when we change the timezone manually on windows 7 using set date time window whether timechange event is triggered or not? The answer is YES it triggered but i am printing current time which is not showing properly..

    static void Main(string[] args)
    {
        SystemEvents.TimeChanged += new EventHandler(SystemEvents_TimeChanged)开发者_开发技巧;
        Console.Read();
    }

    static void SystemEvents_TimeChanged(object sender, EventArgs e)
    {
        Console.WriteLine(DateTime.Now);
    }

Once you run console app and then try to change the timezone it always reflects one time change but then it somehow stuck to that time even if you change the timezone to different timezone or same.

Am I missing something?

to verify whether system time has changed or not i have opened command prompt and use date and 'time' command to print the current time which shows perfect according to timezone.


I believe the system time zone is being cached. You can clear this cache though:

TimeZoneInfo.ClearCachedData();

Put that just before your DateTime.Now call, and it looks like it works fine. (Works on my machine, anyway :)

EDIT: As noted in comments, it appears that in some cases you also need to call CultureInfo.CurrentCulture.ClearCachedData(). I didn't, but I dare say it doesn't hurt to do so :)


Stupid question, but are you changing the time, or just the time zone? 5:00 is 5:00, the fact that you changed the timezone (and by extension 'moved' the computer an hour or two ahead or behind) won't change the fact that the system clock is set to 5:00.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜