c++c++buildervcltdatetimec++builder-2009

Converting string to TDateTime in C++Builder 2009


I try to convert strings in specific formats to TDateTime using C++Builder 2009:

TDateTime dt, dt2;
TFormatSettings FS, FS2;

UnicodeString datestring = "17/10/2017 13:24:33";
UnicodeString datestring2 = "2017.17.10 13:24:33";

FS.DateSeparator = '/';
FS.ShortDateFormat = "dd/mm/yyyy";
FS.LongTimeFormat = "hh:nn:ss";
FS.TimeSeparator = ':';

FS2.DateSeparator = '.';
FS2.ShortDateFormat = "yyyy.dd.mm";
FS2.LongTimeFormat = "hh:nn:ss";
FS2.TimeSeparator = ':';

try{
    dt = StrToDateTime(datestring, FS);
    dt2 = StrToDateTime(datestring2,FS2);
}catch(EConvertError& e)
{
    int a = 2;
}

Conversion of dt is ok, but conversion of dt2 throws an exception :

''2017.17.10 13:24:33'' is not a valid date and time


Solution

  • Thank you all!

    Ok now I know that, this date formats are unsupported by StrToDateTime. Solution of this problem is, convert and merge Windows ShortDateFormat and LongTimeFormat to format string accepted by strptime() from time.h. Then I use strptime() and create TDateTime from tm struct from time.h. I try to link docs but, in docs isn't any strptime func. I find this func in time.h from CodeGear RTL ver 13. I think this is equivalent to strptime