|
Boost Users : |
From: Jeff Garland (jeff_at_[hidden])
Date: 2006-06-26 22:38:20
Jason Dolan wrote:
> I'm looking to take a string and convert it to a date. The only problem
> is the string can be one of many patterns. i.e. ("%Y%m%d", "%Y-%m-%d",
> "%d/%m/%Y", etc...). It is also possible that the given string will
> fail all pattern matches, and thus return false.
And there's nothing to indicate which pattern it might be?
> Can anyone give me an example of how to do this?
The date input facet can only parse one pattern at a time. So you might need
some way to distinguish between the various types to use the input facet.
Here's the pseudo code sketch for the examples above:
if (date_string.find('-')) {
date_input_facet di1(...);
...
} else if (date_string.find('/')) {
date_input_facet di2(...);
...
}
The problem you are going to have is if you have formats that can't be
distinguished in this fashion. For example:
%Y%m%d
%d%m%Y
There's no parser that can handle this case.
> I know you need to use the date_input_facet, but I am VERY new to
> boost and can't get it to work.
Did you look at the docs? It basically comes down to:
using namespace boost::gregorian;
date_input_facet* input_facet = new date_input_facet("%Y-%m-%d);
std::istringstream iss("2006-06-01");
iss.imbue(std::locale(std::locale::classic(), input_facet));
date t;
in >> t;
Note, this won't work at all on old compilers...so best to tell me your
compiler / version of boost if you are having compile issues.
Jeff
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net