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

Improvements to BsonDateTime class

    • Type: Icon: Improvement Improvement
    • Resolution: Done
    • Priority: Icon: Minor - P4 Minor - P4
    • 1.8
    • Affects Version/s: 1.7
    • Component/s: None
    • Labels:
      None
    • Minor Change

      Make a few small improvements to BsonDateTime:

      • DateTime constructor should throw if dateTime is not already in UTC
      • remove ToLocalDateTime and ToUniversalTime helper methods
      • remove Value property (keep MillisecondsSinceEpoch)

      The reason we are going to require that the DateTime passed to the constructor already be in UTC is to emphasize that the database only stores UTC values. If you are passing a local time you probably have a bug anyway (or at least would have wondered why the value you got back when reading it back from the database was different), and this exception alerts you to the fact that you are not working in UTC as required by MongoDB.

      Recommended changes if you were are affected by any of these:

      var d = new BsonDateTime(dateTime.ToUniversalTime());
      
      var lt = ((DateTime)v).ToLocalTime();
      var utc = (DateTime)v; // it's already in UTC
      
      var dt = (DateTime)v; // instead of ((BsonDateTime)v).Value
      

            Assignee:
            craig.wilson@mongodb.com Craig Wilson
            Reporter:
            robert@mongodb.com Robert Stam
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: