Forum > FPC development

DateUtils.Inc* with double instead on Int64

(1/9) > >>

tt:
Hi,
I was using the DateUtils.IncSeconds(time, seconds) function, but my seconds parameter ended up being a floating point value.
I remembered that these function want a Int64 number, so I had to use milliseconds with a little rounding.

Would it be senseful to extend them to floating point inputs instead of interger?
All in all the inner calculation does


--- 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 IncSecond(const AValue: TDateTime; const ANumberOfSeconds: Int64): TDateTime;begin  if AValue>=0 then    Result:=AValue+ANumberOfSeconds/SecsPerDay  else    Result:=IncNegativeTime(Avalue,ANumberOfSeconds/SecsPerDay);  MaybeSkipTimeWarp(AValue,Result);end; 
and this would work also with ANumberOfSeconds being a floating point.

Of course the same argument applies to all other DateUtils.Inc* functions.

AlexTP:
This must be Delphi compatible,
https://docwiki.embarcadero.com/Libraries/Sydney/en/System.DateUtils.IncSecond
but if overload will appear, maybe its OK.

tt:
I correct myself:


--- Quote ---The same argument might apply to DateUtils.IncDay, DateUtils.IncHour, DateUtils.IncSecond, DateUtils.IncMilliSecond functions.

--- End quote ---

Others make less sense to be floating point.

winni:
Hi!

If you know the basics of TDateTime thren you don't need IncSeconds and friends.


--- 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";}};} ---Type TDateTime = Double; 
The Integer part contains the days since 30. December 1899

The fractional part contains the time and is organized this way:

1 hour = 1.0 / 24
1 minute = 1.0 / (24*60)
1 second = 1.0/ (24*60*60)

To increment your DateTime with one second you can 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";}};} ---MyDateTime := MyDateTime + 1/ (24*60*60);
To increment your DateTime with 45 days you can 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";}};} ---MyDateTime := MyDateTime +  45; 
Winni

Remy Lebeau:

--- Quote from: tt on May 25, 2022, 01:44:40 pm ---All in all the inner calculation does


--- 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 IncSecond(const AValue: TDateTime; const ANumberOfSeconds: Int64): TDateTime;begin  if AValue>=0 then    Result:=AValue+ANumberOfSeconds/SecsPerDay  else    Result:=IncNegativeTime(Avalue,ANumberOfSeconds/SecsPerDay);  MaybeSkipTimeWarp(AValue,Result);end; 
--- End quote ---

Delphi stopped relying on floating-point math in the DateUtils unit over a decade ago, it was causing too many problems (rounding issues, etc).  Now, most of the date/time calculations are performed by first converting TDateTime to TTimeStamp, manipulating the TTimeStamp as needed using integer math, and then converting the result back to TDateTime.  Much more accurate, albeit maybe slightly less efficient.

Navigation

[0] Message Index

[#] Next page

Go to full version