1

I am using C++ to develop an appointment system.

Can I please have some help in converting a string to a TDateTime variable.

Here is an example that I would like some help with;

TDateTime appDateTime(2012, 9, 15, 14, 0, 0, 0);

I am saving this to a file (with other string and boolean values). When loaded in as a string, the string value is:

15/09/2012 2:00:00 pm

How can I convert this into a TDateTime variable?

thanks

UPDATE

I am trying to create the TDateTime variable via a constructor that uses a string. Here is my code:

    string stringTestConstructorDate = "12/05/1990";

TDateTime testConstruct(stringTestConstructorDate);

I am getting this error: E2285 Could not find a match for 'TDateTime::TDateTime(string)'

How can I use a string variable to construct the TDateTime variable?

Here is my code for a second option:

string stringDateToConvert = "17/09/2012 09:00:02 a.m.";

struct tm DateTime;

if (NULL == strptime("17/09/2012 09:00:02 a.m.", "%D %T", &DateTime)) {

  printf("strptime() failed.\n");

  exit(EXIT_FAILURE);

}

printf("tm_sec  = %3d\n", DateTime.tm_sec );

printf("tm_min  = %3d\n", DateTime.tm_min );

printf("tm_hour = %3d\n", DateTime.tm_hour);

printf("tm_mday = %3d\n", DateTime.tm_mday);

printf("tm_mon  = %3d\n", DateTime.tm_mon );

printf("tm_year = %3d\n", DateTime.tm_year);

This works well. However, can I please have some help in enabling the 'stringDateToConvert' to be passed to the 'strptime' function. Currently I am getting the following error:

E2034 Cannot convert 'string' to 'const char *'

Thanks

Darryl Janecek
  • 399
  • 4
  • 9
  • 25
  • Which library is `TDateTime` from? – jrok Sep 16 '12 at 11:11
  • `TDateTime` is VCL so this presumably is Embarcadero C++. – David Heffernan Sep 16 '12 at 11:13
  • Have you used strptime(http://pubs.opengroup.org/onlinepubs/009695399/functions/strptime.html) – Arpit Garg Sep 16 '12 at 11:17
  • 1
    Well, then there's a [constructor that takes a string.](http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/delphivclwin32/System__TDateTime__TDateTime.html) – jrok Sep 16 '12 at 11:19
  • @jrok: How do I use the TDateTime constructor using a string... I am having trouble with this. Can you please post an example. – Darryl Janecek Sep 17 '12 at 05:39
  • Try `TDateTime testConstruct(stringTestConstructorDate.c_str());` or pass literal directly to the constructor. – jrok Sep 20 '12 at 08:20

1 Answers1

0

There is a StrToDateTime-Function in SysUtils.

http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/delphivclwin32/SysUtils_StrToDateTime@string.html

There is also a StrToDateTimeDef-Function that takes a default value for the case that the parsing fails.

http://docwiki.embarcadero.com/Libraries/XE4/de/System.SysUtils.StrToDateTimeDef

Both of them have overloads that accept a TFormatSettings-Object which can be used to provide metadata for parsing Dates with certain locale formats.

Andreas Pircher
  • 481
  • 4
  • 7