Creating daylight savings-aware DateTime when server set to UTC

azure, c#, datetime, dst, utc

Solution

The main thing you are missing is that "Greenwich Standard Time" is not the `TimeZoneInfo` id for London. That one actually belongs to "(UTC) Monrovia, Reykjavik".

You want "GMT Standard Time", which maps to "(UTC) Dublin, Edinburgh, Lisbon, London"

Yes, Windows time zones are weird. (At least you don't live in France, which gets strangely labeled as "Romance Standard Time"!)

Also, you should not be doing this part:

return DateTime.SpecifyKind(userDateTime, DateTimeKind.Local);

That will make various other code think it came from the local machine's time zone. Leave it with the "Unspecified" kind. (Or even better, use a `DateTimeOffset` instead of a `DateTime`)

Then you also need to use the `.IsDaylightSavingsTime` method on the `TimeZoneInfo` object, rather than the one on the `.DateTime` object. (There are two different methods, with the same name, on different objects, with differing behavior. Ouch!)

But I wholeheartedly agree that this is way too complicated and error prone. Just use Noda Time. You'll be glad you did!

Problem

My application is hosted on Windows Azure, which has all servers set to UTC. I need to know when any given `DateTime` is subject to daylight savings time. For simplicity, we can assume that my users are all in the UK (so using Greenwich Mean Time). The code I am using to convert my `DateTime` objects is ``` public static DateTime UtcToUserTimeZone(DateTime dateTime) { dateTime = DateTime.SpecifyKind(dateTime, DateTimeKind.Utc); var timeZone = TimeZoneInfo.FindSystemTimeZoneById("Greenwich Standard Time"); var userDateTime = TimeZoneInfo.ConvertTime(dateTime, timeZone); return DateTime.SpecifyKind(userDateTime, DateTimeKind.Local); } ``` However, the following test fails at the last line; the converted `DateTime` does not know that it should be in daylight savings time. ``` [Test] public void UtcToUserTimeZoneShouldWork() { var utcDateTime = new DateTime(2014, 6, 1, 12, 0, 0, DateTimeKind.Utc); Assert.IsFalse(utcDateTime.IsDaylightSavingTime()); var dateTime = Utils.UtcToUserTimeZone(utcDateTime); Assert.IsTrue(dateTime.IsDaylightSavingTime()); } ``` Note that this only fails when my Windows time zone is set to (UTC) Co-ordinated Universal Time. When it is set to (UTC) Dublin, Edinburgh, Lisbon, London (or any other northern-hemisphere time zone that observes daylight savings), the test passes. If you change this setting in Windows a restart of Visual Studio is required in order for the change to fully take effect. What am I missing?

Original source

Related problems