Created
April 30, 2016 01:05
-
-
Save jeremyfromearth/5694aa3a66714254752179ecf3c95582 to your computer and use it in GitHub Desktop.
strptime for C++
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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