Godspped
Godspped

Reputation: 743

How to convert date string to time_t

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++.

Upvotes: 2

Views: 5211

Answers (1)

Justin Randall
Justin Randall

Reputation: 2278

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;
}

Upvotes: 3

Related Questions