runtime: DateTime.Now returns incorrect time on windows computer when time zone has changed

Issue Title

DateTime.Now returns incorrect time on windows computer when time zone has changed

General

Details

I was running a windows service created in C# when I noticed my computer was set to the wrong time zone. I switched it to the correct timezone and waited for my service to do something. It did not. I restarted the service and it worked correctly. I created a test app in C# and noticed that If I change my timezone, DateTime.Now does not use the updated time. I then created a test app in .NET Core to see if it had the same behavior, and it does.

.NET Core version

.NET Core 2.1

Operating System version

Windows 10 Pro - 1903 (18362.207)

Test Code

using System;

namespace DateTimeNowTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(DateTime.Now);
            Console.ReadLine();
            //Before pressing enter, change time zone
            Console.WriteLine(DateTime.Now);
        }
    }
}

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 1
  • Comments: 19 (12 by maintainers)

Most upvoted comments

@julealgon take Windows OS as example, if want to listen to the system time change notification, at the bottom you need to get it through a message pump. Look at https://learn.microsoft.com/en-us/windows/win32/sysinfo/wm-timechange. To have a message pump, you need to have a Window to register the message pump. To have a Window, you need to have a thread that loop on this message pump. Look at the SystemEvents.TimeChanged remarks which says the following:

This event is only raised if the message pump is running. In a Windows service, unless a hidden form is used or the message pump has been started manually, this event will not be raised. For a code example that shows how to handle system events by using a hidden form in a Windows service, see the SystemEvents class.

There are other ways to listen to the system time notification, like in WinRT, WinRT and most UWP apps are built on top of a message-driven architecture. Events, such as system time changes, are generally dispatched through the application’s message pump. This ensures that events are processed on the same thread that owns the message loop, typically the UI thread in most apps. Or you need to create your dedicated thread to listen to the event.

iirc this was done intentionally to avoid issues with bad timing code that compares DateTime instances. You can work around it by doing this at app startup:

SystemEvents.TimeChanged += (s,e) => TimeZoneInfo.ClearCachedData();

Thank you so much for the added context @tarekgh !

… because I closed it right before that, it wasn’t something you had closed. I guess more I was wanting to let you close this if it’s truly going to be “as designed”.

… hopefully you’re sending more than just the local time, or your stored data will be nonsense.
Assuming future dates, you need to be storing local time + offset + zone id (past dates can always be UTC). Failure to store one of these pieces will result in update errors when the DST or other offset rules change.

For ease and speed of querying, a combined type should be preferred for the first two pieces (eg, DateTimeOffset from SQL Server), as usually these will work as UTC under the covers.