15

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.

Aabela
  • 1,408
  • 5
  • 19
  • 28
  • 1
    You do realize that London is not on GMT, right? As it is now summer in the Northern hemisphere, London is on UTC+1 while you're on UTC+2. – Gabe Jul 03 '12 at 11:33
  • See what my problem is ? I don't want to mark it as BST (which is the current time) because when it stops being BST it goes back to GMT. I'm hoping its automatic. I HATE timezones. – Aabela Jul 03 '12 at 11:36
  • Fortunately for you, it will automatically use BST for times in the summer and GMT for times in the winter. You're right that `TimeZone` has issues, but that's what `TimeZoneInfo` was created. – Gabe Jul 03 '12 at 11:54
  • One issue is that some timezones have *really* weird names in windows and .net. For example `Europe/London` is misnamed `GMT Standard Time` and switches between summer and winter time. But GMT does not which is called `Greenwich Standard Time` in .net. http://stackoverflow.com/questions/2292334/difference-between-utc-and-gmt-standard-time-in-net – CodesInChaos Jul 03 '12 at 12:16
  • Timekeeping is a whole damn textbook if you want to get it exactly right. Microsoft doesn't even try for Israel due to the forty or so religious DST variations that change every damn year (can't celebrate at the wrong time just because of daylight savings, Jehovah might get his nose out of joint and start smiting! I cannot understand how a nation as tech savvy as Israel can be so silly but there it is.) GMT (Greenwich Mean Time) is almost but not exactly the same as UTC (Universal Time Convention) which is almost but not exactly the same as Sidereal time. It's mayhem! – Peter Wone Jul 04 '12 at 07:07

4 Answers4

3

The short answer is "Not everywhere, not perfectly."


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.

Peter Wone
  • 17,965
  • 12
  • 82
  • 134
  • 1
    Am I understanding it correctly as "No, you will have to write code to check for it yourself" ? Or is it automatic? – Aabela Jul 03 '12 at 11:31
  • However much the tidal forces are slowing the planet, this does not accurately answer the question: "does TimeZoneInfo take DST into consideration?" to which the answer is actually: "Yes and you don't need to write any code for this yourself, at least at .net 4.5". – pasx Jul 10 '15 at 05:50
  • @pasx - I wrote this in July of 2012. When was 4.5 released? August 2012. Lucky for you there's no way to downvote foolish comments. – Peter Wone Jul 10 '15 at 12:14
  • 1
    @PeterWone TimeZoneInfo was also in 3.5 which was released in 2008. Is there a way to downvote foolish answers to comments? (: But honestly I downvoted your answer because it gives me a lot of information I don't need and the information which is relevant to converting with DST is erroneous. There is no code to write for conversion with DST. – pasx Jul 12 '15 at 07:07
  • 1
    This answer was not written for you, it was written for people interested in the complexities of timekeeping. If the additional information was not useful to you that says nothing about others. This may come as a shock but not all of the internet was created for your personal convenience, and your disinterest in *why* it's all so complicated does not diminish the value of the information to others. I find it telling that instead of writing a helpful answer - something you clearly could have done - you preferred to denigrate someone else's contribution. – Peter Wone Jul 12 '15 at 11:16
  • A caveat, what if DST changes, as it does from time to time in different countries. Will we have to recompile the app with the latest version of .net to get the updates? – niico Feb 21 '21 at 14:25
  • @niico that's a really good question and I don't know the answer. – Peter Wone Aug 25 '21 at 04:44
0

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.

wal
  • 17,409
  • 8
  • 74
  • 109
  • 2
    It means I'm obtaining the date from a provider who is giving me London time, and I need to normalise it with another date from a provider who gives it to me in my local timezone. I try to avoid these problems but sometimes there's nothing to do. I don't want to change the clock to find out because the last time I did that many things went wrong with project references for some reason (something to do with build times). – Aabela Jul 03 '12 at 12:26
  • @Aabela that is not a good enough reason not to change your clock on your computer. you are the master of your computer. change the clock, test, then change it back. you can clean your solution (under the Build menu) which should correct that. If you still are worried test it on a virtual machine – wal Jul 03 '12 at 12:29
  • you should convert both of those times to UTC and work with them in UTC. – wal Jul 03 '12 at 12:30
0

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/

ash
  • 236
  • 1
  • 7
0

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.

pasx
  • 2,718
  • 1
  • 34
  • 26