title: Standard conformant way of converting std::time_t to System::DateTime?
body: <p>I have already found several answers related to converting a <code>std::time_t</code> value to <code>System::DateTime</code> and back. However, almost all answers seem to neglect that the type of <code>std::time_t</code> is actually undefined in the standard. Most solutions just cast <code>std::time_t</code> to whatever needed or apply arithmetic operations to a <code>std::time_t</code> object which is possible since it's an arithmetic type, but there is no specification about the result of such an operation. I know that <em>most</em> compilers define <code>time_t</code> as an <code>int</code> of some size but the fact alone that it has changed from <code>int32</code> to <code>int64</code> in many implementations recently shows that changes are indeed possible. </p>
  
  <p>So I've come up with this solution which <em>should</em> work with any type of <code>std::time_t</code>. It works from what I have seen. But I was wondering - <strong>Are there any possible pitfalls I might be unaware of?</strong></p>
  
  <pre><code>template &lt;&gt;
  inline System::DateTime marshal_as(const std::time_t &amp;from_object)
  {
      // Returns DateTime in Local time format from time_t (assumed to be UTC)
      const auto unix_epoch = makeUtcTime(1970, 1, 1, 0, 0, 0);
      const auto unix_epoch_dt = System::DateTime(1970, 1, 1, 0, 0, 0, System::DateTimeKind::Utc);
      const auto secondsSinceEpoch = std::difftime(from_object, unix_epoch);
      return const_cast&lt;System::DateTime&amp;&gt;(unix_epoch_dt).AddSeconds(secondsSinceEpoch).ToLocalTime();
  } // end of System::DateTime marshal_as(const std::time_t &amp;from_object)
  
  template &lt;&gt;
  inline std::time_t marshal_as(const System::DateTime &amp;from_object)
  {
      // Returns time_t in UTC format from DateTime
      auto from_dt = const_cast&lt;System::DateTime&amp;&gt;(from_object).ToUniversalTime();
      return makeUtcTime(from_dt.Year, from_dt.Month, from_dt.Day, from_dt.Hour, from_dt.Minute, from_dt.Second);
  } // end of std::time_t marshal_as(const System::DateTime &amp;from_object)
  </code></pre>
  
  <p>3 assumptions were made:</p>
  
  <ul>
  <li>Resulting <code>std::time_t</code> should be in UTC since it doesn't contain any info on localization</li>
  <li>Resulting <code>System::DateTime</code> should be local time since <code>System::DateTime::Now</code> returns a localized <code>DateTime</code></li>
  <li><code>makeUtcTime</code> is a helper function creating a <code>std::tm</code> from the values supplied and creates a UTC <code>std::time_t</code> out of it. This is currently implemented using <code>_mkgmtime</code> because our interop code can safely rely on the existence of Microsoft extensions. However, a UTC version of <code>mktime</code> is readily available in other compilers as well (standard <code>mktime</code> expects local time). </li>
  </ul>
  
  <p>2 less important things to consider: </p>
  
  <ul>
  <li>The <code>const_cast</code> is necessary because the marshal_as-template expects a <code>const T&amp;</code> as parameter and I can't access the properties of a const .NET value-type object. However there might be a better solution. </li>
  <li>Should the <code>unix_epoch...</code> stuff be <code>static const</code>?</li>
  </ul>
  
  <p>(I wasn't sure if this should be posted on "Programmers Exchange" since it's more of a discussion but since it's a very specific C++ question I thought SO might be the better place to ask)</p>