1

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
  • 129
  • 1
  • 7
  • 2
    Possible duplicate of [How to parse date/time from string?](https://stackoverflow.com/questions/3786201/how-to-parse-date-time-from-string) – Melebius Nov 29 '17 at 07:49
  • 2
    _“I don't want to pass format date in a function.”_ So the function should _guess_ whether the format is MM/DD/YYYY, or DD/MM/YYYY? I definitely wouldn’t rely on that. – Melebius Nov 29 '17 at 07:51
  • `_variant_t` does it – Rikitikitavi Nov 29 '17 at 07:53

1 Answers1

0

If you want to use the local date format you can try std::get_time (available since C++11) or the C function strftime 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,683
  • 2
  • 13
  • 31