Darien Pardinas
Darien Pardinas

Reputation: 6186

Is there a standard way to convert a std::string to std::chrono::time_point without using Boost?

Basically I'm looking for a standard way to convert a string like 2014/08/29-11:42:05.042 into a time_point object. I know how to do it with boost, but can it be done with STL libraries only? How?

It would be great if if could specify the format like %y/%m/%d-%H:%M:%S.%f or something alike.

Upvotes: 1

Views: 1119

Answers (1)

Darien Pardinas
Darien Pardinas

Reputation: 6186

Ok, at least for a fixed format with millisecond resolution this works. Attempting to make this code capable of accepting any string format will be like reinventing the wheel (i.e. there are functions for all this in Boost.

std::chrono::system_clock::time_point string_to_time_point(const std::string &str)
{
    using namespace std;
    using namespace std::chrono;

    int yyyy, mm, dd, HH, MM, SS, fff;

    char scanf_format[] = "%4d.%2d.%2d-%2d.%2d.%2d.%3d";

    sscanf(str.c_str(), scanf_format, &yyyy, &mm, &dd, &HH, &MM, &SS, &fff);

    tm ttm = tm();
    ttm.tm_year = yyyy - 1900; // Year since 1900
    ttm.tm_mon = mm - 1; // Month since January 
    ttm.tm_mday = dd; // Day of the month [1-31]
    ttm.tm_hour = HH; // Hour of the day [00-23]
    ttm.tm_min = MM;
    ttm.tm_sec = SS;

    time_t ttime_t = mktime(&ttm);

    system_clock::time_point time_point_result = std::chrono::system_clock::from_time_t(ttime_t);

    time_point_result += std::chrono::milliseconds(fff);
    return time_point_result;
}

std::string time_point_to_string(std::chrono::system_clock::time_point &tp)
{
    using namespace std;
    using namespace std::chrono;

    auto ttime_t = system_clock::to_time_t(tp);
    auto tp_sec = system_clock::from_time_t(ttime_t);
    milliseconds ms = duration_cast<milliseconds>(tp - tp_sec);

    std::tm * ttm = localtime(&ttime_t);

    char date_time_format[] = "%Y.%m.%d-%H.%M.%S";

    char time_str[] = "yyyy.mm.dd.HH-MM.SS.fff";

    strftime(time_str, strlen(time_str), date_time_format, ttm);

    string result(time_str);
    result.append(".");
    result.append(to_string(ms.count()));

    return result;
}

To test it I tried like this and made sure that the string correctly represented the current date time:

auto tp_src = system_clock::now();
string value = time_point_to_string(tp_src);
auto tp_cnv = string_to_time_point(value);
auto error = duration_cast<milliseconds>(tp_src - tp_cnv).count();
Assert::IsTrue(error == 0);

Upvotes: 1

Related Questions