title: C#: Custom time zone format strings
body: <p>I need to read the timezone offset from UTC from a custom format. This is defined per standard as:</p>
  <ul>
  <li><a href="http://dicom.nema.org/medical/dicom/current/output/chtml/part03/sect_C.12.html#sect_C.12.1.1.8" rel="nofollow noreferrer">http://dicom.nema.org/medical/dicom/current/output/chtml/part03/sect_C.12.html#sect_C.12.1.1.8</a></li>
  </ul>
  <blockquote>
  <p>Encoded as an ASCII string in the format &quot;&amp;ZZXX&quot;. The components of
  this string, from left to right, are &amp; = &quot;+&quot; or &quot;-&quot;, and ZZ = Hours
  and XX = Minutes of offset. Leading space characters shall not be
  present.</p>
  <p>The offset for UTC shall be +0000; -0000 shall not be used.</p>
  </blockquote>
  <p>I fail to understand how I can use any of the modifiers in my case to read this format (and only this format):</p>
  <ul>
  <li><a href="https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings</a></li>
  </ul>
  <p>So far I have:</p>
  <pre><code>bool isValidTimezoneOffsetFromUTC(string timezoneOffsetFromUTC)
  {
      try
      {
          DateTimeOffset.ParseExact(timezoneOffsetFromUTC, &quot;%K&quot;, CultureInfo.InvariantCulture);
          if (timezoneOffsetFromUTC.Length != 5 || timezoneOffsetFromUTC == &quot;-0000&quot;) return false;
          return true;
      }
      catch (ArgumentNullException)
      {
  
      }
      catch (FormatException)
      {
  
      }
      return false;
  }
  </code></pre>
  <p>Which seems overly complex...is there a way to simplify this code without external dependencies ?</p>