title: .Net DateTime with local time and DST
body: <p>I'm afraid I don't really understand how .Net's <code>DateTime</code> class handles local timestamps (I live in Germany, so my locale is de_DE). Perhaps someone can enlighten me a bit ;-)</p>
  
  <p>The <code>DateTime</code> constructor can be called with year, month etc. parameters. Additionally a <code>DateTimeKind</code> value of <code>Local</code>, <code>Utc</code>, or <code>Unspecified</code> (=default) can be provided.</p>
  
  <p>Example:</p>
  
  <pre><code>DateTime a = new DateTime(2015, 03, 29, 02, 30, 00, DateTimeKind.Local);
  DateTime b = new DateTime(2015, 03, 29, 02, 30, 00, DateTimeKind.Utc);
  DateTime c = new DateTime(2015, 03, 29, 02, 30, 00, DateTimeKind.Unspecified);
  DateTime d = new DateTime(2015, 03, 29, 02, 30, 00);
  </code></pre>
  
  <p>Per definition, values c and d are identical. But if I compare all against each other, all four are identical. Inspecting the objects in VS's debugger shows that the <code>Ticks</code> value (and <code>InternalTicks</code> as well) is the same for all. However, internal <code>dateData</code> values are different, but are obviously ignored by the comparison operator.</p>
  
  <p>As you might have noticed, I constructed a value for March 29th this year, 02:30 AM. This moment in time does not exist in our timezone as it is skipped by switching to Daylight Saving Time. So I had expected to get an exception for constructing object <code>a</code>, but this did not happen.</p>
  
  <p>Furthermore, <code>DateTime</code> has a method <code>ToUniversalTime()</code> that converts a value that is interpreted as local time to the equivalent UTC value. For testing, I ran a loop as follows:</p>
  
  <pre><code>DateTime dt = new DateTime(2015, 03, 29, 01, 58, 00, DateTimeKind.Local);
  DateTime dtEnd = new DateTime(2015, 03, 29, 03, 03, 00, DateTimeKind.Local);
  while (dt &lt; dtEnd)
  {
      Log(" Localtime " + dt + " converted to UTC is " + dt.ToUniversalTime());
      dt = dt.AddMinutes(1);
  }
  </code></pre>
  
  <p>The result is:</p>
  
  <pre><code>Localtime 29.03.2015 01:58:00 converted to UTC is 29.03.2015 00:58:00
  Localtime 29.03.2015 01:59:00 converted to UTC is 29.03.2015 00:59:00
  Localtime 29.03.2015 02:00:00 converted to UTC is 29.03.2015 01:00:00
  Localtime 29.03.2015 02:01:00 converted to UTC is 29.03.2015 01:01:00
  Localtime 29.03.2015 02:02:00 converted to UTC is 29.03.2015 01:02:00
  ...
  Localtime 29.03.2015 02:58:00 converted to UTC is 29.03.2015 01:58:00
  Localtime 29.03.2015 02:59:00 converted to UTC is 29.03.2015 01:59:00
  Localtime 29.03.2015 03:00:00 converted to UTC is 29.03.2015 01:00:00
  Localtime 29.03.2015 03:01:00 converted to UTC is 29.03.2015 01:01:00
  Localtime 29.03.2015 03:02:00 converted to UTC is 29.03.2015 01:02:00
  </code></pre>
  
  <p>So, .Net has no problem converting non-existing timestamps from local time to UTC. Also, adding a minute to an existing local timestamp is not local-aware and gives a non-existing timestamp.</p>
  
  <p>As a result, adding 64 single minutes yields, after conversion, a UTC timestamp that is only 4 minutes larger than before.</p>
  
  <p>In other words, converting between local time and UTC should be a bijection, giving a one-to-one correspondence between legal timestamp values.</p>
  
  <p>To cut a long story short: How do I handle this correctly the intended way (according to .Net)? What is the sense of having <code>DateTimeKind</code> if it is not taken into account correctly? I don't even dare to ask how leap seconds (at 23:59:60) are handled ;-)</p>