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
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;
Without using facets;
ptime dateTime = boost::date_time::parse_delimited_time<ptime>(string, 'T');
The two from*_string
functions have limits on what formats are converted.
time_from_string(s)
.from_iso_string(s)
.Roundtripping ISO 8601 date/time in boost;
std::string date = to_iso_extended_string(dateTime);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With