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

The same BsonDateTime were stored different timezone

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 2.7.3
    • Component/s: BSON
    • Labels:
    • Environment:
      Window Client. with .NET Core 2

      I'm using C# Driver 2.7 and MongoDB 4.0.x.

      The issue I'm encounted with is about the date field. Basically, I know the date is stored in UTC. "16:22:37.661" is local time and "2019-03-12 08:22:37.661" is its equivelent UTC. How come it shows different in two places?

      My json looks like:

      {
        _id: ObjectId("5c8716ac4304b7e199a4b830"),
        key: "xyz",
        details: [
          {...},
          {...},
          {
              v: 1,
              data: "....",
              ts: 2019-03-12 16:22:37.661
          }
        ],
        create_ts: 2019-03-12 02:16:57.256,
        details_ts: 2019-03-12 08:22:37.661
      }
      

       

      The json submitted from client contains only key and one or elements in details. The server-side upserts it and pushes (accumulates) new details into the array. Before pushing, the time stamp will be added to each element of the detail array then set the latest ts of details_ts. The issue is the ts in the last element of details looks different from details_ts. It doesn't make sense to me because I create only one BsonDateTime and use many places. They should be look exact the same no matter what's the timezone. My C# Code is like:

       

      DateTimeOffset dt = DateTimeOffset.Now;
      BsonDateTime bdt = new BsonDateTime(dt.UtcDateTime);
      
      //creating filter
      ......
      
      var update_builder = Builders<BsonDocument>.Update;
      var update_list = new List<UpdateDefinition<BsonDocument>>();
      update_list.Add(update_builder.SetOnInsert("create_ts", bdt));
      
      var details = doc["details"].AsBsonArray;
      foreach (BsonDocument d in details) {
          d.Set("ts", bdt);
      }
      update_list.Add(update_builder.Set("detail_ts", bdt));
      update_list.Add(update_builder.PushEach("details", details));
      
      var updater = update_list.Count > 0 ? update_builder.Combine(update_list.ToList()) : update_builder.Combine();
      var options = new UpdateOptions { IsUpsert = true };
      
      var r = await collection.WithWriteConcern(wc).UpdateOneAsync(filter, updater, options);
      

       

            Assignee:
            wan.bachtiar@mongodb.com Wan Bachtiar
            Reporter:
            jsnhsu@msn.com Jason Hsu
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: