title: Converting between time zones with Noda Time
body: <p>I'm currently trying to ensure that our legacy back-end can support resolving date times based on the user's current time zone (or, more specifically offset).  Our servers are in eastern standard time, and most of our date times originate there.  However, for users that are in other time zones, a conversion to their time zone (or, in this case, offset) is needed when retrieving those date times.  Also, date times coming from the user will have to be converted to eastern standard time before persistence on the server.  Given that the front end we are developing is web-based, I am able to retrieve the user's offset in minutes and pass that value into my service layer within the header.  I looked at Noda Time and think it's a great API.  It did force me to think about time in a more refined matter, but I am still not 100% sure that I've properly used it correctly.  Here are the methods that I wrote for the conversions described above.  I've tested them and they seem to work.  Given the scenario above, does this look like a proper use of the library?  Am I thinking about date times properly?</p>
  
  <pre><code>public static DateTime ConvertToUtcFromEasternTimeZone(DateTime easternDateTime)
  {
      NodaTime.DateTimeZone easternTimeZone = NodaTime.DateTimeZoneProviders.Tzdb.GetZoneOrNull("America/New_York");
      ZoneLocalMappingResolver customResolver = Resolvers.CreateMappingResolver(Resolvers.ReturnLater, Resolvers.ReturnStartOfIntervalAfter);
      var easternLocalDateTime = LocalDateTime.FromDateTime(easternDateTime);
      var easternZonedDateTime = easternTimeZone.ResolveLocal(easternLocalDateTime, customResolver);
      return easternZonedDateTime.ToDateTimeUtc();
  }
  
  public static DateTime ConvertToEasternTimeZoneFromUtc(DateTime utcDateTime)
  {
      NodaTime.DateTimeZone easternTimeZone = NodaTime.DateTimeZoneProviders.Tzdb.GetZoneOrNull("America/New_York");
      NodaTime.DateTimeZone utcTimeZone = NodaTime.DateTimeZoneProviders.Tzdb.GetZoneOrNull("UTC");
      ZoneLocalMappingResolver customResolver = Resolvers.CreateMappingResolver(Resolvers.ReturnLater, Resolvers.ReturnStartOfIntervalAfter);
      var utcLocal = LocalDateTime.FromDateTime(utcDateTime);
      var utcZonedDateTime = utcTimeZone.ResolveLocal(utcLocal, customResolver);
      var easternZonedDateTime = utcZonedDateTime.ToInstant().InZone(easternTimeZone);
      return easternZonedDateTime.ToDateTimeUnspecified();
  }
  
  public static DateTime ConvertToUtc(DateTime dateTime, int offsetInMinutes)
  {
      LocalDateTime localDateTime = LocalDateTime.FromDateTime(dateTime);
      var convertedDateTime = localDateTime.PlusMinutes(offsetInMinutes).ToDateTimeUnspecified();
      return convertedDateTime;
  }
  
  public static DateTime ConvertFromUtc(DateTime dateTime, int offsetInMinutes)
  {
      LocalDateTime localDateTime = LocalDateTime.FromDateTime(dateTime);
      var convertedDateTime = localDateTime.PlusMinutes(-offsetInMinutes).ToDateTimeUnspecified();
      return convertedDateTime;
  }
  </code></pre>
  
  <p>The idea here is that time zone matters when I'm resolving between UTC time and the time zone in the database.  When I'm resolving between the client time and UTC time then offset matters.  </p>
  
  <p>In the future, we can persist UTC time, and this will be easier.  Currently, this solution is a stop gap.</p>
  
  <p>The idea is that we are going to go from...</p>
  
  <p>client -> UTC +/- offset -> UTC -> Eastern Time -> database</p>
  
  <p>database -> Eastern Time -> UTC -> UTC +/- offset -> client</p>
  
  <p>to eventually...</p>
  
  <p>client -> UTC +/- offset -> UTC  -> database</p>
  
  <p>database -> UTC -> UTC +/- offset -> client</p>