Created
June 13, 2014 04:00
-
-
Save mihids/e3b545cd7b00003d27b6 to your computer and use it in GitHub Desktop.
Convert a string in the 24h format HH:mm:ss to time_t
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
#include <time.h> | |
#include <string> | |
#include <stdlib.h> | |
inline time_t StringToTime(std::string time_24h) { | |
time_t theTime = time(NULL); | |
struct tm *aTime = localtime(&theTime); | |
std::string delimiter = ":"; | |
size_t pos = 0; | |
std::string token; | |
while ((pos = time_24h.find(delimiter)) != std::string::npos) { | |
token = time_24h.substr(0, pos); | |
aTime->tm_hour = atoi(token.c_str()); | |
time_24h.erase(0, pos + delimiter.length()); | |
} | |
aTime->tm_min = atoi(time_24h.c_str()); | |
aTime->tm_sec = 0; | |
return mktime(aTime); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment