This function will convert a time in the HH:mm to a time_t object, but the seconds will be neglected.
e.g. 13:14 (std::string) -> 13:14 (time_t)
13:14:23 (std::string) -> 14:23 (time_t)
e.g. 13:14 (std::string) -> 13:14 (time_t)
13:14:23 (std::string) -> 14:23 (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); | |
} |
Comments
Post a Comment