Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SystemEvent.TimeChange showing same time even timezone changes

Tags:

timezone

c#

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.

like image 740
user393014 Avatar asked Sep 21 '11 21:09

user393014


People also ask

Why is my computer showing the wrong time?

In Windows, Network Time Protocol (NTP) is responsible for displaying accurate time. It synchronises your system clock's time with that of other computer servers by communicating on the internet. When this synchronisation setting is not properly configured, it can lead to inaccurate time. Check the Service status.

Can Windows automatically adjust time zone?

In Date & time, you can choose to let Windows 10 set your time and time zone automatically, or you can set them manually. To set your time and time zone in Windows 10, go to Start > Settings > Time & language > Date & time.


1 Answers

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 :)

like image 89
Jon Skeet Avatar answered Nov 15 '22 17:11

Jon Skeet