Search code examples
delphidelphi-xe2delphi-2006formatdatetime

Delphi XE2 FormatDateTime passing -693594


We have a bit of an issue here. We have upgraded from Delphi 2006 to Delphi XE2 and are in the process of converting our code.

The problem is, we use the value -693594 through our application and database records to represent no date (zero date). In Delphi 2006 the FormatDateTime function would correctly format this as 00/00/0000 (given a date format of dd/mm/yyyy).

However in Delphi XE2 they have added a call to ValidateTimeStampDate in the DateTImeToTimeStamp function in System.SysUtils which raises the error "invalid floating point operation". passing anything greater than -693594, such as -693593, works fine.

Has anyone else had this issue and/or does anyone know a work around?


Solution

  • If you are really desperate to patch back to the previous behaviour you could use something like this:

    unit Unit1;
    
    interface
    
    uses
      Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
      Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
    
    type
      TForm1 = class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      end;
    
    var
      Form1: TForm1;
    
    implementation
    
    {$R *.dfm}
    
    procedure PatchCode(Address: Pointer; const NewCode; Size: Integer);
    var
      OldProtect: DWORD;
    begin
      if VirtualProtect(Address, Size, PAGE_EXECUTE_READWRITE, OldProtect) then
      begin
        Move(NewCode, Address^, Size);
        FlushInstructionCache(GetCurrentProcess, Address, Size);
        VirtualProtect(Address, Size, OldProtect, @OldProtect);
      end;
    end;
    
    type
      PInstruction = ^TInstruction;
      TInstruction = packed record
        Opcode: Byte;
        Offset: Integer;
      end;
    
    procedure RedirectProcedure(OldAddress, NewAddress: Pointer);
    var
      NewCode: TInstruction;
    begin
      NewCode.Opcode := $E9;//jump relative
      NewCode.Offset := NativeInt(NewAddress)-NativeInt(OldAddress)-SizeOf(NewCode);
      PatchCode(OldAddress, NewCode, SizeOf(NewCode));
    end;
    
    function DateTimeToTimeStamp(DateTime: TDateTime): TTimeStamp;
    const
      FMSecsPerDay: Single = MSecsPerDay;
      IMSecsPerDay: Integer = MSecsPerDay;
    var
      LTemp, LTemp2: Int64;
    begin
      LTemp := Round(DateTime * FMSecsPerDay);
      LTemp2 := (LTemp div IMSecsPerDay);
      Result.Date := DateDelta + LTemp2;
      Result.Time := Abs(LTemp) mod IMSecsPerDay;
    end;
    
    procedure TForm1.Button1Click(Sender: TObject);
    begin
      ShowMessage(FormatDateTime('dd/mm/yyyy', -693594));
    end;
    
    initialization
      RedirectProcedure(@System.SysUtils.DateTimeToTimeStamp, @DateTimeToTimeStamp);
    
    end.
    

    This will work for 32 bit code. It will also work for 64 bit code provided that both the old and new functions reside in the same executable module. Otherwise the jump distance may exceed the range of a 32 bit integer. It will also not work if your RTL resides in a runtime package. Both of these limitations can be readily remedied.

    What this code does is re-route all calls to SysUtils.DateTimeToTimeStamp to the version implemented in this unit. The code in this unit is just the PUREPASCAL version from the XE2 source.

    The only other approach that meets the needs outlined in your comments is to modify and re-compile the SysUtils unit itself, but I personally avoid that sort of solution.

    enter image description here