Error DateTime ToLocalTime

I am working with a MongoDB database. I know when you insert a DateTime in Mongo, it will convert it to UTC. But I am doing a unit test, and my Assert is not working.

[TestMethod] public void MongoDateConversion() { DateTime beforeInsert = DateTime.Now; DateTime afterInsert; Car entity = new Car { Name = "Putt putt", LastTimestamp = beforeInsert }; // insert 'entity' // update 'entity' from the database afterInsert = entity.LastTimestamp.ToLocalTime(); Assert.AreEqual(beforeInsert, afterInsert); // fails here } 

I have to miss something obvious. When I look in the debugger, I see that the date and time match, but the statement still says no (but they do):

 Result Message: Assert.AreEqual failed. Expected:<5/21/2015 8:27:04 PM>. Actual:<5/21/2015 8:27:04 PM>. 

Any ideas what I'm doing wrong here?

EDIT:

I came up with two possible solutions, both of which require that I remember to do something (which is not always best to rely on ...):

One of them is to use the extension method to trim any DateTime coming out of the database:

 public static DateTime Truncate(this DateTime dateTime) { var timeSpan = TimeSpan.FromMilliseconds(1); var ticks = -(dateTime.Ticks % timeSpan.Ticks); return dateTime.AddTicks(ticks); } 

The other, after reading http://alexmg.com/datetime-precision-with-mongodb-and-the-c-driver/ , should mark any DateTime value in the POCO class:

 public class Car : IEntity { public Guid Id { get; set; } [BsonDateTimeOptions(Representation = BsonType.Document)] public DateTime LastTimestamp { get; set; } } 
+6
source share
3 answers

MongoDB saves DateTimes as a 64-bit millisecond since the UNIX era. See this page: http://alexmg.com/datetime-precision-with-mongodb-and-the-c-driver/

Since the .NET DateTime resolution is 100 nanoseconds, MongoDB will almost certainly truncate any DateTime time when you round trip like this.

You have several options.

Option 1: Make sure you truncate LastTimestamp when you set it, or before inserting a record:

 long excessTicks = timestamp.Ticks % 10000; timestamp= new DateTime( ticks: timestamp.Ticks - excessTicks, kind: timestamp.Kind ); 

This approach will be error prone. Anyone who sets LastTimestamp should be aware of truncation, or you may truncate the value just before inserting the record, but you may need to unexpectedly modify the CLR object.

Alternatively, you can use getter / setter and just trim the LastTimestamp to milliseconds each time it is installed. But this can lead to the fact that another unit test will continue to work with an error for the same reason as in this test.

Option 2: If accuracy in milliseconds is not important, just add some tolerance to your statement:

 TimeSpan delta = beforeInsert - afterInsert; Assert.IsTrue(Math.Abs(delta.TotalMilliseconds) <= 2); 
+1
source

I assume you have a difference in milliseconds. Try trimming the millisecond as shown below:

 DateTime d = DateTime.Now; d = d.AddMilliseconds(-d.Millisecond); 
+1
source

Your test is wrong (or inconsistent) because there will always and always will be a difference of a few milliseconds before and after insertion. You could just convert the date and time to a string formatted as dd/mm/yy hh:mm:ss and compare before and after

+1
source

Source: https://habr.com/ru/post/987705/


All Articles