I am trying to convert date-times that i am receiving into a particular format to insert into a MySQL database. The program is written in C++, and the following solution works but I feel it is horribly inefficient.
The input is : Mon Nov 08 17:41:23 +0000 2010
The desired output format is: YYYY-MM-DD HH:MM:SS
So for this example the output would be: 2010-11-08 17:41:23
I have included the relevant parts of the code.
//class variable
std::map<std::string, std::string> monthMap;
void processor::initializeMonthMap(){
monthMap["Jan"] = "01";
monthMap["Feb"] = "02";
monthMap["Mar"] = "03";
monthMap["Apr"] = "04";
monthMap["May"] = "05";
monthMap["Jun"] = "06";
monthMap["June"] = "06";
monthMap["Jul"] = "07";
monthMap["July"] = "07";
monthMap["Aug"] = "08";
monthMap["Sept"] = "09";
monthMap["Sep"] = "09";
monthMap["Oct"] = "10";
monthMap["Nov"] = "11";
monthMap["Dec"] = "12";
}
inline std::string processor::convertDate(std::string input) {
//Format: Mon Nov 08 17:41:23 +0000 2010
//To get to YYYY-MM-DD HH:MM:SS
std::stringstream newString(input);
std::string temp1;
std::string temp2;
// Read Day in txt, discard
newString >> temp1;
//Read month, convert to number
newString >> temp1;
temp2 = "-" + monthMap[temp1] + "-";
//Read Day in number
newString >> temp1;
temp2.append(temp1 + " ");
//Read TimeStamp
newString >> temp1;
temp2.append(temp1);
//Discard UTM adjustment
newString >> temp1;
//Read year
newString >> temp1;
//Add year to beginning of input
temp1.append(temp2);
return temp1;
}