[CSHARP-2545] The same BsonDateTime were stored different timezone Created: 14/Mar/19  Updated: 25/Jun/20  Resolved: 25/Jun/20

Status: Closed
Project: C# Driver
Component/s: BSON
Affects Version/s: 2.7.3
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Jason Hsu Assignee: Wan Bachtiar
Resolution: Done Votes: 0
Labels: Bug, rfw
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Window Client. with .NET Core 2



 Description   

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);

 



 Comments   
Comment by Rachelle Palmer [ 25/Jun/20 ]

Hi there, thank you for reaching out to MongoDB. As this sounds more like a support issue, I wanted to give you some resources to get this question answered more quickly:

  • our MongoDB support portal, located at support.mongodb.com
  • our MongoDB community forums, located here
  • If you are an Atlas customer, there is free support offered 24/7 in the lower right hand corner of the UI.

Thank you!
Rachelle

Generated at Wed Feb 07 21:42:51 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.