Does TimeZoneInfo take DST into consideration?

2019-06-15 14:21发布

问题:

Does C# take Daylight savings time into consideration when converting between timezones?

I have a source date which is in the current time in London, and I want to convert it to my timezone (CET). Here's the code I'm using.

DateTime time = DateTime.ParseExact(timeString, "HH:mm", null);
time = DateTime.SpecifyKind(time, DateTimeKind.Unspecified);

//Convert it to the right timezone. It is currently in GMT

TimeZoneInfo gmt = TimeZoneInfo.FindSystemTimeZoneById("GMT Standard Time");
TimeZoneInfo current = TimeZoneInfo.Local;

DateTime utc = TimeZoneInfo.ConvertTimeToUtc(time, gmt);
DateTime local = TimeZoneInfo.ConvertTimeFromUtc(utc, core.startTime = local;

It's currently working well. However when DST rears its ugly head, will it continue working or will it break horribly? I'm a bit wary of TimeZones due to having had tons of issues in the past.

回答1:

TimeZoneInfo.GetAdjustmentRules will give you a collection of rules about changes in the DST offset and when they come into and go out of effect.

However, your user can still cock things up by un-checking "Automatically adjust for daylight savings" in Windows Control Panel Date and Time. If DST is turned off in Windows then you will get an empty collection of adjustment rules.

If you want automagical application of adjustment rules you must use DateTime objects for which the DateTimeKind has been set. If DST is turned off this will be honoured in the conversion.

GMT is solar time at the Royal Observatory in Greenwich. The British invented the whole business of timezone offsets from a date-line because they were the first to coordinate anything on a global scale. In halcyon days of yore they had a planet-wide navy of sailboats and no radio. Lag on orders was weeks or months, so consistent, precise, global time-keeping was invented by the only people with a frame of reference larger than a planet - the Royal Astronomers.

The moon's tidal forces are slowing the Earth's rotation. It takes a lot of juice to slosh an ocean of water up and down and it's not magic, it comes from the spin moment of the planet.

Also the duration of a solar orbit isn't constant either, so we have leap seconds every now and then to synch the calendar with planetary reality. Sidereal time on the other hand has no such foolishness, so we drift away from it. Then there is relativistic drift. GPS satellites move so fast they actually have to compensate for slight time-warping.



回答2:

Does C# take Daylight savings time into consideration when converting between timezones?

Yes, assuming your computer is kept updated as the timezone info is sometimes updated with windows update. It should still work even without windows update if the country hasn't changed their DST time periods (this happened in Australia recently)

I have a source date which is in the current time in London, and I want to convert it to my timezone (CET)

What do you mean 'source date which is the current time in London' ? Always store your dates as UTC and convert them 'at the last minute' to the desired local time.

If you're wondering what happens when daylight savings changes then you can test this by changing the clock on your computer.



回答3:

Be careful when working with dates before 1987 in .NET. Default AdjustmentRules in TimeZoneInfo for the time zone that you are interested in may not be sufficient for your purpose. Read more here : http://blog.appliedis.com/2013/03/06/beware-daylight-saving-time-transitions-in-dot-net/



回答4:

At least in .net 4.5 TimeZoneInfo does handle daylight saving time.

The easiest way to check it is to compare BaseUtcOffset and GetUtcOffset

   var baseOffset = timeZoneInfo.BaseUtcOffset;
   var currentOffset = timeZoneInfo.GetUtcOffset(currentLocalTime);
   var isDst = currentOffset > baseOffset;
   var delta = currentOffset - baseOffset;

This is much easier than dealing with AdjustmentRule which you don't need if you are only interested in adjusting a DateTime for DST.

Btw GMT is obsolete and is replaced by UTC.



标签: c# timezone dst