Skip to content

Instantly share code, notes, and snippets.

@mihids
Created June 13, 2014 04:00
Show Gist options
  • Save mihids/e3b545cd7b00003d27b6 to your computer and use it in GitHub Desktop.
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
#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