error with using strptime in c++
I've tried to understand use of strptime() in c++ and to be able to do it, I write a short code :
#include <time.h>
#include <iostream>
Test(string dtime)
{
string s = dtime;
struct tm timeDate;
strptime(s,"%y-%m-%d %H:%M", &timeDate);
cout<<timeDate.tm_sec<<endl;
}
dtime will always be like 2013:03:15 16:08. I got the error it says that :
Test(std::string)’:
Test.cpp:17:41: error: cannot convert ‘std::string* {aka std::basic_string<char>*}’ to ‘const char*’ for argument ‘1’ to ‘char* strptime(const char*, const char*, tm*)
Is there anyone to help me to solve my problem ? Thanks in advance
Solution 1:
The declaration of strptime
is:
char *strptime(const char *s, const char *format, struct tm *tm);
The first argument expects a const char *
. You are passing a std::string
Convert s
to a C style char *
strptime(s.c_str(),"%y-%m-%d %H:%M", &timeDate);