0

I have a string which is date. Is there a function from standart library which can parse it in any numeric format(e.g timestamp or julian day)

For example

 std::string str("01/01/2017");//Local format date
 UINT64 timestamp = function(str);

I don't want to pass format date in a function. I want to know only that a string represented in local format.

Rikitikitavi
  • 133
  • 1
  • 8

1 Answers1

-1

If you want to use the local date format you can try std::get_time (available since C++11)

When used in an expression in >> get_time(tmb, fmt), parses the character input as a date/time value according to format string fmt according to the std::time_get facet of the locale currently imbued in the input stream in.

or the POSIX C function strptime with "x" as format.

| x | parses the locale's standard date representation | all | -- cppreference.com

Both return calendar time struct tm that you can convert it to timestamp using either localtime or gmtime if you want.

Note that you have to set the C and C++ locale of your application before, in order to be able to use your OS regional settings and not the classic C locale. See locale and std::locale. And it's good to set both so that they are in sync and not get bad surprises.

setlocale(LC_ALL, "");
std::locale::global(std::locale(setlocale(LC_ALL, NULL)));
Mihayl
  • 3,821
  • 2
  • 13
  • 32