Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disparity between date/time calculations in C# versus Delphi

Delphi:

SecondsBetween(StrToDateTime('16/02/2009 11:25:34 p.m.'), StrToDateTime('1/01/2005 12:00:00 a.m.'));

130289133

C#:

TimeSpan span = DateTime.Parse("16/02/2009 11:25:34 p.m.").Subtract(DateTime.Parse("1/01/2005 12:00:00 a.m."));

130289134

It's not consistent either. Some dates will add up the same, ie..

TimeSpan span = DateTime.Parse("16/11/2011 11:25:43 p.m.").Subtract(DateTime.Parse("1/01/2005 12:00:00 a.m."));

SecondsBetween(StrToDateTime('16/11/2011 11:25:43 p.m.'), StrToDateTime('1/01/2005 12:00:00 a.m.'));

both give

216905143

The total amount of seconds is actually being used to encode data, and I'm trying to port the application to C#, so even one second completely throws everything off.

Can anybody explain the disparity? And is there a way to get c# to match delphi?

Edit: In response to suggestions that it might be leap second related: Both date ranges contain the same amount of leap seconds (2), so you would expect a mismatch for both. But instead we're seeing inconsistency

16/02/2009 - 1/01/2005 = Delphi and C# calculate a different total seconds

16/11/2011 - 1/01/2005 = They calculate the same total seconds
like image 226
NoPyGod Avatar asked Dec 16 '11 03:12

NoPyGod


People also ask

How can I compare two dates in C?

Consider the problem of comparison of two valid dates d1 and d2. There are three possible outcomes of this comparison: d1 == d2 (dates are equal), d1 > d2 (date d1 is greater, i.e., occurs after d2) and d1 < d2(date d1 is smaller, i.e., occurs before d2).

How do you calculate elapsed time in C?

To get the elapsed time, we can get the time using clock() at the beginning, and at the end of the tasks, then subtract the values to get the differences. After that, we will divide the difference by CLOCK_PER_SEC (Number of clock ticks per second) to get the processor time.

How do I find the difference between two dates in the same column?

To find the difference between dates, use the DATEDIFF(datepart, startdate, enddate) function. The datepart argument defines the part of the date/datetime in which you'd like to express the difference. Its value can be year , quarter , month , day , minute , etc.


1 Answers

The issue it seems related to this QC 59310, the bug was fixed in Delphi XE.

like image 86
RRUZ Avatar answered Oct 11 '22 02:10

RRUZ