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;
由于 HoursPerDay
为 24
,很明显该实现与文档不一致。
这甚至不是随着时间的推移而改变的行为。 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/