This is a follow up to a previous question: Disparity between date/time calculations in C# versus Delphi
I am porting an enterprise application written in Delphi to C#. The application uses a very rudimentary form of encryption for storing the text files it generates. This encryption is based around the Delphi SecondsBetween command, which returns the number of seconds between two dates.
The problem for me is that in the older version of Delphi (the one I’m porting from), there is a bug with the SecondsBetween command which causes it to return values that are off by one – but only about 50% of the time
The bug is a rounding bug. Delphi originally used Trunc instead of Round. See more detail here – http://qc.embarcadero.com/wc/qcmain.aspx?d=59310
Here is the code to demonstrate the problem:
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
What I’d like to do is figure out how I can emulate this buggy behavior in C# so that I can read/write the text files written by the Delphi application.
This is translation of the required functions (SpanOfNowAndThen, SecondSpan) to replicate the Delphi 2007
SecondsBetweenfunction.