C++ boost date format
I have a vector string of dates in the from "dd-mmm-yyyy" so for example todays date would be:
std::string today("07-Sep-2010");
I'd like to use the date class in boost but to create a date object the constructor for date needs to be called as follows:
date test(2010,Sep,07);
Is there any ea开发者_C百科sy/elegant way of passing dates in the format "dd-mmm-yyyy"? My first thought was to use substr and then cast it? But I've read that there's also the possibility of using 'date facets'?
Thanks!
include "boost/date_time/gregorian/parsers.hpp"
date test = boost::gregorian::from_us_string("07-Sep-2010")
There is a builtin parser for this form of date in Boost itself, check out the docs here:
http://www.boost.org/doc/libs/1_44_0/doc/html/date_time/date_time_io.html#date_time.io_objects
date_type parse_date(...) Parameters: string_type input string_type format special_values_parser Parse a date from the given input using the given format.
string inp("2005-Apr-15");
string format("%Y-%b-%d");
date d;
d = parser.parse_date(inp,
format,
svp);
// d == 2005-Apr-15
with inp
adjusted for your needs.
精彩评论