[CSHARP-567] Improvements to BsonDateTime class Created: 10/Sep/12  Updated: 14/May/14  Resolved: 11/Sep/12

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: 1.7
Fix Version/s: 1.8

Type: Improvement Priority: Minor - P4
Reporter: Robert Stam Assignee: Craig Wilson
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
is depended on by CSHARP-357 Add a way to convert all DateTime val... Closed
Backwards Compatibility: Minor Change

 Description   

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



 Comments   
Comment by auto [ 11/Dec/12 ]

Author:

{u'date': u'2012-09-11T15:37:29Z', u'email': u'craiggwilson@gmail.com', u'name': u'Craig Wilson'}

Message: CSHARP-567: removed Value property from BsonDateTime.

Conflicts:
MongoDB.Bson/ObjectModel/BsonDateTime.cs
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/25f4bd7654f8cd713b52e699dec51bed43d4d44c

Comment by auto [ 11/Sep/12 ]

Author:

{u'date': u'2012-09-11T08:37:29-07:00', u'email': u'craiggwilson@gmail.com', u'name': u'Craig Wilson'}

Message: CSHARP-567: removed Value property from BsonDateTime.
Branch: x2.0
https://github.com/mongodb/mongo-csharp-driver/commit/a64d86effa95060fee7062c61fc35d5cf3f1d1ac

Comment by Robert Stam [ 10/Sep/12 ]

Related to this change, also remove AsLocalTime and AsUniversalTime from BsonValue.

The workarounds are identical to those presented above for ToLocalTime and ToUniversalTime.

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