I am trying to convert "Tue Feb 13 00:26:36 2018" to time_t
, how can I do this, I tried looking for a function and couldn't find it. This is using unmanaged C++.
Asked
Active
Viewed 4,503 times
1
-
1Take a look at http://en.cppreference.com/w/cpp/io/manip/get_time or http://en.cppreference.com/w/cpp/io/manip/put_time and http://en.cppreference.com/w/cpp/chrono/c/time – rbf Feb 15 '18 at 05:59
-
possible duplicates: https://stackoverflow.com/questions/11213326/how-to-convert-a-string-variable-containing-time-to-time-t-type-in-c and https://stackoverflow.com/questions/11218797/convert-date-and-time-numbers-to-time-t-and-specify-the-timezone – Eziz Durdyyev Feb 15 '18 at 08:00
-
Possible duplicate of [How to convert a string variable containing time to time\_t type in c++?](https://stackoverflow.com/questions/11213326/how-to-convert-a-string-variable-containing-time-to-time-t-type-in-c) – zett42 Feb 15 '18 at 11:00
1 Answers
3
If you are using C++11, you can do it like this.
#include <iostream>
#include <iomanip>
#include <ctime>
#include <sstream>
int main()
{
struct tm tm;
std::istringstream iss("Tue Feb 13 00:26:36 2018");
iss >> std::get_time(&tm, "%a %b %d %H:%M:%S %Y");
time_t time = mktime(&tm);
std::cout << time << std::endl;
return 0;
}

Justin Randall
- 2,243
- 2
- 16
- 23