title: Converting SYSTEMTIME (Native C++) to DateTime (C#)
body: <p>I have a C++ client talking to a C# server and I have custom serialization for user defined types.</p>
  
  <p>Now I need to pass a date and time structure  from C++ to C#. Googling led to the following approach:</p>
  
  <ol>
  <li>Fill up the date/time in SYSTEMTIME structure (This is not the current system time. But rather it will be filled up by the program based on certain conditions)</li>
  <li>Convert SYSTEMTIME to FILETIME</li>
  <li>Convert FILETIME to __int64 (this gives a 64 bit value representing the number of 100 nanoseconds interval from Jan 1, 1601)</li>
  <li>Use this 64 bit value in the constructor to DateTime in C#.</li>
  </ol>
  
  <p>Just to test the waters, I wrote 2 snippets one in (native)C++ that does steps 1,2,3 and the other in C# that does Step4. No automatic invocation from C++ to C# yet. </p>
  
  <p>I manually pulled out the value from step 3 (130220830133980000L) and used it in C# and here is code snippet and the output I get.</p>
  
  <p><strong>C++ Code</strong></p>
  
  <pre><code>SYSTEMTIME st;
  
  GetSystemTime(&amp;st);
  
  printf("\n In C++ : %04d:%02d:%02d:%02d:%02d:%02d:%03d\n", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
  
  FILETIME fileTime;
  if(!SystemTimeToFileTime(&amp;st, &amp;fileTime)) {
      wcout &lt;&lt; "Conversion from system time to file time failed. Error : " &lt;&lt; GetLastError();
  }
  
  __int64 ticks = (((ULONGLONG) fileTime.dwHighDateTime) &lt;&lt; 32) + fileTime.dwLowDateTime;
  </code></pre>
  
  <p><strong>C# Code</strong></p>
  
  <pre><code>DateTime dob = new DateTime(130220830133980000L, DateTimeKind.Utc);
  Console.WriteLine("In C# : " + dob.Year + ":" + dob.Month + ":" + dob.Day + ":" + dob.Hour + ":" + dob.Minute + ":" + dob.Second + ":" + dob.Millisecond);
  </code></pre>
  
  <p><strong>The output:</strong></p>
  
  <p>In C++ : 2013:08:27:13:16:53:398</p>
  
  <p>In C# : 413:8:27:13:16:53:398</p>
  
  <p>All the values except the year part can be retrieved correctly. The year 2013 in C++ becomes 413 in C#. I can't figure out why the year part changes.</p>
  
  <p>Am I doing the conversion correctly? If there is any alternate approach to pass date and time information from C++ to C# and vice versa?</p>