Forum > FPC development

DateUtils.Inc* with double instead on Int64

<< < (2/9) > >>

wp:

--- Quote from: winni on May 25, 2022, 05:49:22 pm ---If you know the basics of TDateTime thren you don't need IncSeconds and friends.[...]

--- End quote ---
Your summary is correct. However, this simple calculation results in an "inverted" time when the date is negative. This case is covered correctly by the IncXXX functions:

--- 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";}};} ---program Project1; uses  SysUtils, DateUtils;  var  t, t0: TDateTime;begin  t0 := EncodeTime(9, 0, 0, 0);   // 9:00 (AM). But note that the date part is 0  WriteLn(FormatDateTime('hh:nn', t0));    // 1 day earlier   // We're expecting the time to be 9:00 again. But the simple subtraction makes it 15:00 (3:00 PM)  // This is because the date part is negative now.  t := t0-1.0;  WriteLn(FormatDateTime('hh:nn', t));    // Now the same calculation but using the IncDay function -> the result is correct.  t := IncDay(t0, -1);  WriteLn(FormatDateTime('hh:nn', t));    ReadLn;end.

winni:
Hi!

But that is not a special problem of TDateTime:

You  allways run in trouble with negative time and/or date.

The greatest disadvantage of TDateTime is the offset date  30. December 1899.
This means you can use TDateTime for office use but it is impossible to do some historical  research before 1900 with TDateTime.

They should have taken an offset date 1.1.0001  or - even better -  use the scientific Julian Days starting at 1.1. -4712  (4713 BC).

Winni

tt:

--- Quote from: winni on May 25, 2022, 05:49:22 pm ---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

--- End quote ---

You are right with the direct calculations, I also did so several times.

But lately I migrated to IncXXX because I perceived them comfortable, reader friendly, and able to manage transparently the oddities of the TDateTime calculations.

I was just wondering if was senseful to extend them allowing fractional quantities increment/decrement, like add 2.58 seconds or 7.652 hours.

SymbolicFrank:
There's about 2^62 seconds in the lifespan of the universe. Fits a single Int64. So set 0000-01-01T00:00 to be 0. Universal time.

The main frustration: leap seconds. Although time zones will be as usual.

MarkMLl:
Pardon me for jumping in, but can anybody comment on why TDateTime isn't an (80-bit) extended on platforms that support it?

MarkMLl

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version