delphidatetimevariants

VarToDateAsString return unexpected result


Delphi has this function:

function VarToDateAsString(const V: TVarData): TDateTime;
....
LResult := VarDateFromStr(S, VAR_LOCALE_USER_DEFAULT, 0, Result);

doing this with today's date:

   VarDateFromStr('07;12;18', VAR_LOCALE_USER_DEFAULT, 0, Result);

then Result will be equal

Result = '07;18;12'

Is any fix possible ?

CODE EXAMPLE

procedure TForm1.Button1Click(Sender: TObject);
var
  LDateTimeVar: Variant;
  LDateTime: TDateTime;
begin
  // Current date separator in OS settings is ';'
  LDateTimeVar := '07;12;18';
  LDateTime := VarToDateTime(LDateTimeVar);
  // Expected LDateTime = '07;12;18', 
  // but will be LDateTime = '07;18;12'  
  ShowMessage(DateToStr(LDateTime));
end;

Solution

  • VarToDateAsString calls 'oleaut32's VarDateFromStr to perform the conversion, which simply fails if you've got the year in the middle.

    variants.VarToDateAsString:

    function VarToDateAsString(const V: TVarData): TDateTime;
    var
      ...
    begin
      _VarToWStr(S, V);
      LResult := VarDateFromStr(S, VAR_LOCALE_USER_DEFAULT, 0, Result);
      ...
    

    Put a breakpoint on the second line and you'll see the Result parameter will reverse the day and the year. You can carry out tests yourself by calling activex.VarDateFromStr.

    You can bypass variants.VarToDateAsString calling VarDateFromStr and implement your own function, but don't think using something from sysutils because it doesn't support a year-in the middle format too. sysutils.TryStrToDateTime calls ScanDate which in turn calls GetDateOrder. The below is the entire function:

    function GetDateOrder(const DateFormat: string): TDateOrder;
    var
      I: Integer;
    begin
      Result := doMDY;
      I := 1;
      while I <= Length(DateFormat) do
      begin
        case Chr(Ord(DateFormat[I]) and $DF) of
          'E': Result := doYMD;
          'Y': Result := doYMD;
          'M': Result := doMDY;
          'D': Result := doDMY;
        else
          Inc(I);
          Continue;
        end;
        Exit;
      end;
    end;
    

    As you can see there's no outcome ordering with year in the middle.


    Looks like you have to parse the string yourself:

    uses
      varutils;
    
    function MyVarDateFromStr(const strIn: WideString; LCID: DWORD; dwFlags: Longint;
      out dateOut: TDateTime): HRESULT; stdcall;
    begin
      // write your parser here and return a valid 'dateOut'
    end;
    
    
    procedure TForm1.Button1Click(Sender: TObject);
    var
      LDateTimeVar: Variant;
      LDateTime: TDateTime;
    begin
      varutils.VarDateFromStr := MyVarDateFromStr;   // replace the broken function
    
      // Current date separator in OS settings is ';'
      LDateTimeVar := '07;12;18';
      LDateTime := VarToDateTime(LDateTimeVar);
      // Expected LDateTime = '07;12;18',
      // but will be LDateTime = '07;18;12'
      ShowMessage(DateToStr(LDateTime));
    end;