Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-357

Add a way to convert all DateTime values in a BsonDocument to local or universal time

    • Type: Icon: Improvement Improvement
    • Resolution: Done
    • Priority: Icon: Minor - P4 Minor - P4
    • 1.4
    • Affects Version/s: 1.3
    • Component/s: None
    • None
    • Minor Change

      When a BsonDocument is read back from the database all DateTime values are in UTC. It would be nice to have a helper method that walked the BsonDocument and converted all DateTime values to LocalTime. For example:

      var document = collection.FindOne(); // DateTimes are in UTC
      document.ToLocalTime(); // converts any DateTime values found to LocalTime (in place)

      While less frequently useful, for symmetry there should probably be a ToUniversalTime method as well.

      An alternative to consider is whether there should be a mode (on the collection?) that would cause all DateTimes to be automatically converted to LocalTime without having to call a helper method. Although the helper method is by far simpler to implement.

      As an aside, I recommend that you always keep your DateTimes in UTC throughout, and only convert them to LocalTime when displaying them to the user. But I know that many developers prefer to just do a wholesale conversion to LocalTime after reading from the database, so this helper method would be attractive to them.

            Assignee:
            robert@mongodb.com Robert Stam
            Reporter:
            robert@mongodb.com Robert Stam
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: