Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Delphi use double to store Date and Time instead of Int64?

Tags:

delphi

Why does Delphi use double (8 bytes) to store date and time instead of Int64 (8 byte as well)? As a double precision floating point is not an exact value, I'm curious wether the precision of a unix date and time stored in an Int64 value will be better than the precision of a Delphi date and time?

like image 366
zeus Avatar asked Jan 16 '18 16:01

zeus


1 Answers

The simple explanation is that the Delphi TDateTime type maps directly to the OLE/COM date/time format.

Embarcadero chose to use an existing date/time representation rather than create yet another one, and selected, what was at the time, the most obvious platform native option.

A couple of useful articles on Windows date/time representations:

  • How to recognize different types of timestamps from quite a long way away, Raymond Chen
  • Eric's Complete Guide To VT_DATE, Eric Lippert

As far as precision goes, you would like to compare Unix time to TDateTime. Unix time has second precision for both 32 or 64 bit values. For values close to the epoch, a double has far greater precision. There are 86,400 seconds in a day, and there are many orders of magnitude more double values between 0 and 1, for instance. You need to go to around year 188,143,673 before the precision of Unix time surpasses that of TDateTime.

Although you have focused on the size of the types, the representation is of course crucially important. For instance, if instead of representing date as seconds after epoch, it was represented as milliseconds after epoch, then the precision of Unix time would be 1000 times greater. Of course the range would be reduced by 1000 times also.

You need to be wary of considering precision of these types in isolation. These types don't exist in isolation, and the source of the values is important. If the time is coming from a file system say, then that will in fact determine the precision of the value.

like image 175
David Heffernan Avatar answered Jan 03 '23 06:01

David Heffernan