[CSHARP-3522] C# driver assumes the DateTime is of kind Local when it's Unspecified Created: 01/Apr/21  Updated: 06/Apr/21  Resolved: 06/Apr/21

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

Type: Bug Priority: Major - P3
Reporter: Diana Zhivkova Assignee: Robert Stam
Resolution: Won't Fix Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

 

// the date is of kind Unspecified
var date = DateTime.Parse("2020-11-20"); 
 
// the filter will render as if the date was of kind Local
var filter = Builders<BsonDocument>.Filter.Eq(it => it.Created, date); 

 
Seems logical to assume the date is of kind Utc in case it is not provided.



 Comments   
Comment by Robert Stam [ 06/Apr/21 ]

I understand that it might be logical to assume DateTime values of Kind Unspecified are in UTC.

The server requires that all DateTime values be in UTC, so we call `date.ToUniversalTime()` to ensure that.

The behavior of `dateTime.ToUniversalTime()` is:

  • if Kind is UTC => return DateTime unchanged
  • if Kind is Local => convert DateTime to UTC
  • if Kind is Unspecifed => convert DateTime to UTC

So we are simply relying on the standard behavior of the DateTime ToUniversal method here.

At this point changing the behavior would be a breaking change, so the reasonable course of action here is to leave the current behavior in place.

 

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