how to get boost::posix_time::ptime from formatted string

boost, c++

Solution

If the existing from_string() methods do not suit your needs then you can use a time input facet that allows you to customise the format from which the string is parsed.

In your case you can use the ISO extended format string so you can use the following code to parse your strings:

    boost::posix_time::time_input_facet *tif = new boost::posix_time::time_input_facet;
    tif->set_iso_extended_format();
    std::istringstream iss("2012-03-28T08:00:00");
    iss.imbue(std::locale(std::locale::classic(), tif));
    iss >> abs_time;
    std::cout << abs_time << std::endl;

Problem

I have a formated string like "2012-03-28T08:00:00". i want to get year, month(in string format),date,hour, min ,sec and day (in string format). can any one suggest me the easiest way to do it in boost. thanks

Original source