title: DateTimeOffset parse and custom time zone
body: <p>We are parsing XML DateTime values into a DateTimeOffset value. According to the <a href="http://www.w3.org/TR/2012/REC-xmlschema11-2-20120405/datatypes.html#dateTime" rel="noreferrer">W3C XSD documentation for DateTime</a>, the type may have a timezone info or not.  </p>
  
  <p>Our requirment says:</p>
  
  <ul>
  <li>If time zone info is provided in XML, use this time zone</li>
  <li>If no time zone info is provided, assume it's a local time in a predefined, configurable time zone (not the one from Server).</li>
  </ul>
  
  <p><strong>The Problem is that when a XML DateTime without a time zone is parsed into a DateTimeOffset, it uses the local (System) timezone by default. It seems not possible to override the default timezone and also not possible to identify wheter the timezone was parsed or added internally.</strong></p>
  
  <p>Is there any way I can specify the default timezone used by the DateTimeOffset parsing?<br>
  If not, how can one identify if the time zone was parsed or added automatically during parsing for DateTimeOffset?</p>
  
  <p>It seems strange to me that there is no support to set the time zone for a .NET application the same way as one can set the current culture.</p>
  
  <p>Therefore the only approach to this problem seems to first parse the value into DateTime and check the Kind property. If Kind is not <em>Unspecified</em>, parse the value again into a DateTimeOffset:  </p>
  
  <pre class="lang-cs prettyprint-override"><code>/*
  sample values:
  - 2015-06-03T10:47:01
  - 2015-06-03T07:47:01Z
  - 2015-06-03T10:47:01+03:00
  */
  
  DateTimeOffset dto;
  var timeZone = TimeZoneInfo.FindSystemTimeZoneById(ConfigurationManager.AppSettings["DefaultTimeZone"]);
  var dt = DateTime.Parse(value);
  
  if (dt.Kind == DateTimeKind.Unspecified)
  {
      dto = new DateTimeOffset(dt, timeZone.GetUtcOffset(dt));
  }
  else
  {
      dto = DateTimeOffset.Parse(value);
  }
  </code></pre>