title: Convert DateTime to Utc only if not already Utc
body: <p>I'm using the DateTimeWithZone struct that Jon Skeet posted at <a href="https://stackoverflow.com/questions/246498/creating-a-datetime-in-a-specific-time-zone-in-c-fx-3-5">Creating a DateTime in a specific Time Zone in c# fx 3.5</a></p>
  
  <p>This didn't work exactly for my situation since it assumes that the DateTime passed in the constructor is the local time, and therefore converts it to Utc using the specified TimeZone.</p>
  
  <p>In my case we will mostly be passing in DateTime objects already in Utc (since this is what we are storing) so we need to only perform the conversion if the source DateTime.Kind is not Utc.</p>
  
  <p>Therefore I changed the constructor to:</p>
  
  <pre><code>    public DateTimeWithZone(DateTime dateTime, TimeZoneInfo timeZone, DateTimeKind kind = DateTimeKind.Utc) {
          dateTime = DateTime.SpecifyKind(dateTime, kind);
          utcDateTime = TimeZoneInfo.ConvertTimeToUtc(dateTime, timeZone);
          this.timeZone = timeZone;
      }
  </code></pre>
  
  <p>Here we have an optional Kind parameter that defaults to Utc.</p>
  
  <p>However, running this code and passing a Utc DateTime generates the following exception:</p>
  
  <blockquote>
    <p>The conversion could not be completed because the supplied DateTime did not have the Kind property set correctly.  For example, when the Kind property is DateTimeKind.Local, the source time zone must be TimeZoneInfo.Local.</p>
  </blockquote>
  
  <p>According to the docs (<a href="http://msdn.microsoft.com/en-us/library/bb495915.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/bb495915.aspx</a>):</p>
  
  <blockquote>
    <p>If the Kind property of the dateTime parameter equals DateTimeKind.Utc and the sourceTimeZone parameter equals TimeZoneInfo.Utc, this method returns dateTime without performing any conversion.</p>
  </blockquote>
  
  <p>Since both the input time and the timezone both have a Kind property of Utc then I would not expect to get this exception.</p>
  
  <p>Have I misunderstood?</p>