如何在C#中模拟Delphi 2007 SecondsBetween函数?
这是上一个问题的后续问题:之间的差异C# 与 Delphi 中的日期/时间计算
我正在将用 Delphi 编写的企业应用程序移植到 C#。该应用程序使用非常基本的加密形式来存储它生成的文本文件。这种加密基于 Delphi SecondsBetween 命令,该命令返回两个日期之间的秒数。
对我来说,问题是在旧版本的 Delphi(我从中移植的版本)中,SecondsBetween 命令存在一个错误,导致它返回相差 1 的值 - 但只有大约 50%该
错误是一个舍入错误。 Delphi 最初使用 Trunc 而不是 Round。在此处查看更多详细信息 - http://qc.embarcadero.com/wc/qcmain .aspx?d=59310
这是演示问题的代码:
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
我想做的是弄清楚如何模拟这种错误行为在 C# 中,以便我可以读取/写入由 Delphi 应用程序编写的文本文件。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是复制 Delphi 2007
SecondsBetween 所需函数(SpanOfNowAndThen、SecondSpan)的翻译
函数。This is translation of the required functions (SpanOfNowAndThen, SecondSpan) to replicate the Delphi 2007
SecondsBetween
function.我认为在您的情况下,我只需将有问题的 Delphi 例程编译成 DLL 并使用 p/invoke 从 C# 调用它。我将在计算之前添加一个调用来设置 8087 控制字,并在计算之后将其恢复为调用者值。这将是我能想到的风险最低的方法。
I think in your situation I would simply compile the faulty Delphi routine into a DLL and use p/invoke to call it from the C#. I'd include a call to set the 8087 control word before the calc, and one to restore it to the callers value afterwards. This would be the lowest risk approach that I can think of.
人为地为要减去的项(右侧)添加 0.5 秒,然后将 TimeSpan.TotalSeconds 截断为 int 值怎么样?也许这会起作用:
How about artificially adding 0.5 seconds to the term being subtracted (right side) and then truncating
TimeSpan.TotalSeconds
to anint
value? Perhaps this would work:业务风险听起来非常高,不幸的是,Delphi 提供的有关此错误内部行为方式的信息一无是处。用 C# 编写和单元测试补偿方法并不能保证始终缓解该问题。
除非知识渊博的人提供具体细节,否则您是否可以使用版本 15.0.3729.28755(包含错误修复)重新编译并推出您的原始 Delphi 应用程序?
另一种可能性是“大爆炸”推出 C# 端口以完全取代 Delphi 端口,尽管这在培训和员工支持方面可能会带来更大压力。
祝你好运——这是一个棘手的问题。
The business risk sounds very high, and unfortunately there's naff-all information from Delphi about how this bug behaves internally. Writing and unit-testing a compensatory method in C# can't be guaranteed to mitigate the issue at all times.
Unless a knowledgeable soul chimes in with specific details, might it be possible for you to recompile and rollout your original Delphi app using version 15.0.3729.28755 (with the bug fix)?
The other possibility is a 'big bang' rollout for your C# port to completely replace the Delphi one, although that can be more stressful in terms of training and staff support.
Good luck with this - it's a tricky problem.