Skip to content

Instantly share code, notes, and snippets.

@jeremyfromearth
Created April 30, 2016 01:05
Show Gist options
  • Save jeremyfromearth/5694aa3a66714254752179ecf3c95582 to your computer and use it in GitHub Desktop.
Save jeremyfromearth/5694aa3a66714254752179ecf3c95582 to your computer and use it in GitHub Desktop.
strptime for C++
struct tm tm;
std::string dateString = "2016-04-29T00:30:29.016448Z"
strptime(dateString.c_str(), "%Y-%m-%dT%H:%M:%SZ", &tm);
int day = tm.tm_mday, month = tm.tm_mon + 1, year = tm.tm_year + 1900, hour = tm.tm_hour, min = tm.tm_min;
extern "C" char* strptime(const char* s, const char* f, struct tm* tm) {
std::istringstream input(s);
input.imbue(std::locale(setlocale(LC_ALL, nullptr)));
input >> std::get_time(tm, f);
if (input.fail()) {
return nullptr;
}
return (char*)(s + input.tellg());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment