Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET AddDays issue

Tags:

c#

.net

datetime

The next 2 lines adds the same amount to the same date, and the results date part is the same, but somehow the there's difference in the time part!

(new DateTime(2000,1,3,18,0,0)).AddDays(4535);   
(new DateTime(2000,1,3,18,0,0)).AddMonths(149);

you'll get a difference of 15 secs, and with both are at least roundable to days, I don't know why this happend, but it happens only with AddDays, but not AddMonths (even with thousands of months added)


Edit 1

So I've tried to make a sample project, but no luck. If I run my main project, and put the sample lines into the watches, than I get 2 separate values, if I make a fresh start, the problem is not there. The project is 3.5, c#, vs2010, win7hp x64 (proj: x86). I'm trying to reproduce it also in a fresh small project, I'll be writing back if I have it.

These are my results in the main project (copeid from watches!):

(new DateTime(2000, 1, 3, 18, 0, 0)).AddDays(4535).Ticks    
 634743432153600000 long

(new DateTime(2000, 1, 3, 18, 0, 0)).AddMonths(149).Ticks   
 634743432000000000 long

Edit 2

I've managed to narrow it down even more. We have a self-made component, panel base, we draw on it with directx. If I make that visible=false, than visible=true, than the error comes, before the visible=true (or show()), the calculation is correct. What in the world can be there, that the result gets something else of a formula where no variable is used. Culture is not affected in the component..

like image 858
user1269009 Avatar asked Mar 14 '12 12:03

user1269009


1 Answers

Here they give the same result:

var d1 = (new DateTime(2000, 1, 3, 18, 0, 0)).AddDays(4535).Ticks;
var d2 = (new DateTime(2000, 1, 3, 18, 0, 0)).AddMonths(149).Ticks;

d1 == d2 == 634743432000000000

(the Tick is the internal "quantum" of time of DateTime. It's quite short. It's one ten-millionth of a second

I'll add that even Mono (an independent implementation of .NET) gives the same result http://ideone.com/krySY (Ideone uses mono)

Considering the more recent things you wrote, it's quite easy: memory corruption. Memory corruption can do very random things. This probably is one of those :-)

like image 145
xanatos Avatar answered Oct 21 '22 22:10

xanatos