Forum > FPC development

Additional opinions on TryEncodeTime and TryEncodeTimeInterval [Swift flavour]

(1/2) > >>

lagprogramming:
rtl/objpas/sysutils/dati.inc.TryEncodeTime contains the following source code:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function TryEncodeTime(Hour, Min, Sec, MSec:word; Out Time : TDateTime) : boolean;begin  Result:=(Hour<24) and (Min<60) and (Sec<60) and (MSec<1000);  If Result then    Time:=TDateTime(cardinal(Hour)*3600000+cardinal(Min)*60000+cardinal(Sec)*1000+MSec)/MSecsPerDay;end;
packages/rtl-objpas/src/inc/dateutil.inc.TryEncodeTimeInterval contains the following source code:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function TryEncodeTimeInterval(Hour, Min, Sec, MSec: word; out Time: TDateTime): boolean;begin Result:= (Min<60) and (Sec<60) and (MSec<=1000); If Result then   Time:=TDateTime(cardinal(Hour)*3600000+cardinal(Min)*60000+cardinal(Sec)*1000+MSec)/MSecsPerDay;end; 
   This inconsistency between result assignments make me feel that there might be a bug in one of these functions. Doesn't look strange to you, too!?

winni:
Hi!

No, there is no inconsistency.

At computing the interval there might be the possibility that hour is >= 24.
As there is no day parameter the computing will automatic wrap it into one or more days.

For the encoding of the time this it not legal.
So everything is okay.

Winni

lagprogramming:
Knowing that 1 second equals 1000miliseconds, 1minute equals 60seconds and 1hour equals 60minutes, why is 1000 a valid value for MSec but 60 is not a valid value for Sec and Min parameters?!
A while ago, TryEncodeTimeInterval used "(MSec<1000);" but the source code has been changed to "(MSec<=1000);".
TryEncodeTimeInterval(0,0,59,1000,Time) returns true while TryEncodeTimeInterval(0,0,60,0,Time) returns false.
If you're sure that there is no problem, so be it. It's just that, to me, it's not too obvious what the result of the function really means.

winni:
Hi!

Yes Mister Millisecond - you are right!

But I am not happy with the restrictions for TryEncodeTimeInterval.

If you are computing the Interval on the base of hours/minutes/seconds/ms you might get an underflow for one or more of the values. But that does not matter: The computing of the TDateTime will resolve that automatic. This restriction only complicates the computing.

But I think this function only exists due to Delphi-compatibility-reasons.

It is much easier to do


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---DateTime_Interval := DateTime_End - DateTime_Start; 
Winni

Zvoni:

--- Quote from: lagprogramming on June 22, 2021, 12:44:59 pm ---Knowing that 1 second equals 1000miliseconds, 1minute equals 60seconds and 1hour equals 60minutes, why is 1000 a valid value for MSec but 60 is not a valid value for Sec and Min parameters?!
A while ago, TryEncodeTimeInterval used "(MSec<1000);" but the source code has been changed to "(MSec<=1000);".
TryEncodeTimeInterval(0,0,59,1000,Time) returns true while TryEncodeTimeInterval(0,0,60,0,Time) returns false.
If you're sure that there is no problem, so be it. It's just that, to me, it's not too obvious what the result of the function really means.

--- End quote ---

Because there is no value "60" for a second (or minute) --> It doesn't exist!

The first minute of the hour is the minute with value "0" (same for seconds)
the "sixtieth" second/minute is the one with value 59
The same for milliseconds: the 1000th millisecond is actually the first millisecond of the next second (equaling 0),
or the other way round: the 0th Millisecond is actually the first

Another clue for your millisecond-"problem" --> Look at all the Time-Format-Strings "hh:mm:ss:zzz" --> only three digits for Millisecond

The Day starts at "00:00:00:000" --> "zero hours:zero minutes:zero seconds:zero milliseconds"

Having said that:
MSec<=1000 is plain wrong

Navigation

[0] Message Index

[#] Next page

Go to full version