Reputation: 193
Consider this code:
var datetime = DateTime.Now;
var instantMessage = InstantMassageingManager.GetConverationMessages().FirstOrDefault();
InstantMassageingManager.UpdateConversationMessageReadDateTime(instantMessage.InstantMessageInstanceId, datetime);
var message = InstantMassageingManager.GetMessageById(instantMessage.InstantMessageInstanceId);
Assert.IsTrue(message.ReadDateTime.Value == datetime);
in the first line I get DateTime.Now
then I update a record in database: UpdateConversationMessageReadDateTime
and get the message again from the database:
Both message.ReadDateTime
and datetime
has same value but with different Tick.
So my test doesn't pass.
Why do I get different values for tick?
Upvotes: 3
Views: 212
Reputation: 1499860
I strongly suspect that the type you're using in the database doesn't have the same level of precision - I suspect it's only accurate to the millisecond.
If you want to round-trip the time, you should truncate your "input" time to the nearest millisecond too. For example:
TimeSpan rounded = TimeSpan.FromMilliseconds(original.Ticks / 10000);
Or for a DateTime
:
DateTime rounded = new DateTime(original.Year, original.Month, original.Day,
original.Hour, original.Minute, original.Second, original.Millisecond,
original.Kind);
You should also consider whether you really want to take the local time (which depends on the system time zone) or a UTC value. It depends on the context, but it's more frequently appropriate to store UTC values for timestamps, which is what it looks like you've got here.
Upvotes: 7