How did I not know this?
-
So I'm working on an Azure app and one thing I do (and have done for 11 years now) is use
DateTime.Now
, which gives me the current system date and time. Never been an issue with WinForms apps or locally hosted apps. It's even never been an issue in web apps where I've used it for logging and the like. I've had to use specific time zones before, but always the user's client time zone. In this particular case, I need to save the system/company date and time and also report this back to the user. Azure uses UTC and I'm in The Netherlands though, so the time is always off by one or two hours (depending on summer or winter). The fix, apparently, is this:var timeZone = TimeZoneInfo.FindSystemTimeZoneById("W. Europe Standard Time");
var now = TimeZoneInfo.ConvertTime(DateTime.UtcNow, timeZone);The "W. Europe Standard Time" is a bit of a magical string that will probably change in a few years when we're abolishing summer time (so make it a setting and don't hard code it!). You can get a list of these magical strings using tzutil /l (on Windows). I've always known DateTime.Now isn't really safe or anything, it's just never been an issue until now. DateTime.Today is specific and accurate enough for almost everything I do. And so I've learned how to get the date and time for a specific timezone after 11 years of programming :)
Best, Sander Azure DevOps Succinctly (free eBook) Azure Serverless Succinctly (free eBook) Migrating Apps to the Cloud with Azure arrgh.js - Bringing LINQ to JavaScript
The same thing happens in Windows Explorer with DST. A file is changed at 10 AM standard time the Friday before DST changes. If you check the time stamp on Monday it will display an hour difference. File times are UTC-ish, it is the display time zone that changes. This was Win 7 behavior; I have not verified in Win 10.