Is the documentation from Embarcadero incorrect for EncodeDateTime?

The documentation for System.DateUtils.EncodeDateTime() says:

Valid hour values ​​are from 0 to 24. (If the specified hour is 24, minutes, seconds, and milliseconds must be 0, and the final TDateTime value is midnight at the end of the specified day and the beginning of the next day).

If I try to do EncodeDateTime(2008,1,1,24,0,0,0); I get an exception.

What am I doing wrong?

+5
source share
1 answer

This is a defect in the documentation. The implementation of TryEncodeTime , which is what the actual work does, is as follows:

 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; 

Since HoursPerDay is 24 , it is clear that the implementation is not consistent with the documentation.

This is not even behavior that has changed over time. The TryEncodeTime method TryEncodeTime always behaved like this. For example, a similar function from Delphi 5 looks like this:

 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; 
+7
source

Source: https://habr.com/ru/post/1276162/


All Articles