Corrected calendar functions

delphimoon

Corrected Delphi calendar functions

Algorithms

Top  Previous  Next

 

Corrected versions of some Delphi calendar functions

 

function IsLeapYearCorrect(year: word):boolean;

function EncodeDateCorrect(year,month,day: word):TDateTime;

procedure DecodeDateCorrect(date: TDateTime; var year,month,day: word);

procedure DecodeTimeCorrect(date: TDateTime; var Hour,Min,Sec,mSec: word);

function FalsifyTDateTime(date:TDateTime):TDateTime;

 

Description

By definition the Delphi TDateTime should be the same as a julian date, that means the number of days since a fixed date (which was changed to December 30th, 1899 since Delphi 2). However, all the internal functions connected with dates (at least all versions until Delphi 6) use a proleptic gregorian calendar, that means they project is gregorian calendar back to times where it was not in effect yet. To make it even worse the fractional part of the TDateTime is handled totally wrong for negative dates (i.e. dates before 1899-12-30, and only since Delphi 2), for example -10.1 should be 21:36 on December 19th 1899, but Delphi makes it 2:24 on the 20th.

So whenever a IsLeapYear, EncodeDate, Decodedate or Decodetime is needed use these corrected versions instead, unless you are sure dates before 1900 will never occur. For example to use the FormatDateTime function there is also the FalsifyTDateTime which modifies the value to get it handled correctly by Delphi.

 

Hint

The switching date between julian and gregorian calendar is the one of the decree of pope Gregor, making October 4th the last day of the julian calendar, followed directly by the 15th. However, the calendar change was adopted at various later times throughout Europe, for example England changed 1752, and Russia in 1918, so these corrected functions might be equally wrong as the original Delphi functions for some historic dates depending on location. For more flexibility the direct calendar functions can be used.