delphi - Embarcadero 的文档中的 EncodeDateTime 是否错误?

标签 delphi

System.DateUtils.EncodeDateTime() 的文档说:

Valid hour values are 0 through 24. (If the specified hour is 24, the minute, second, and millisecond values should all be 0, and the resulting TDateTime value represents midnight at the end of the specified day and the beginning of the next day).

如果我尝试执行 EncodeDateTime(2008,1,1,24,0,0,0); 我会收到异常。

我做错了什么?

最佳答案

这是文档中的缺陷。实际工作的 TryEncodeTime 的实现如下:

function TryEncodeTime(Hour, Min, Sec, MSec: Word; out Time: TDateTime): Boolean;
var
  TS: TTimeStamp;
begin
  Result := False;
  if (Hour < HoursPerDay) and (Min < MinsPerHour) and (Sec < SecsPerMin) 
    and (MSec < MSecsPerSec) then
  begin
    ....
    Result := True;
  end;
end;

由于 HoursPerDay24,很明显该实现与文档不一致。

这甚至不是随着时间的推移而改变的行为。 TryEncodeTime 方法始终以这种方式运行。例如,Delphi 5 中的类似函数如下所示:

function DoEncodeTime(Hour, Min, Sec, MSec: Word; var Time: TDateTime): Boolean;
begin
  Result := False;
  if (Hour < 24) and (Min < 60) and (Sec < 60) and (MSec < 1000) then
  begin
    Time := (Hour * 3600000 + Min * 60000 + Sec * 1000 + MSec) / MSecsPerDay;
    Result := True;
  end;
end;

关于delphi - Embarcadero 的文档中的 EncodeDateTime 是否错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49557789/

相关文章:

delphi - 如何在 TDBGrid 中显示富文本?

delphi - 即使树未聚焦时也突出显示选择

Delphi LoadLibrary 找不到其他目录的 DLL - 有什么好的选择吗?

Json 解析 "Invalid class typecast"问题。 [德尔福XE7]

delphi - Delphi XE2 Listview工件

delphi - Delphi中的 boolean 表达式求值顺序?

Delphi:启动应用程序的快捷方式在哪里?

delphi - 如何将泛型与两个不同的类(其中之一来自 VCL)一起使用

delphi - Delphi 中 RC 文件的依赖性检查

delphi - HttpSendRequest 阻止 SOAP 调用