|
Boost Users : |
From: Yuval Ronen (ronen_yuval_at_[hidden])
Date: 2005-10-28 14:42:20
Jeff Garland wrote:
> On Wed, 26 Oct 2005 09:26:43 +0200, Roman Morokutti wrote
>
>>Is it possible to parse a date out from a string
>>like this? The example below is from a java program.
>>
>> private static final String PLANSTART_PATTERN = "dd.MM.yyyy HH:mm";
>>
> Yes, trivially.
IMHO, not trivial enough for such a trivial (and common) task.
> ptime
> parse_jdate1(const std::string& s)
> {
>
> ptime t;
> try {
> std::stringstream ss(s.c_str());
> time_input_facet* timefacet = new time_input_facet("%d.%m.%Y %H:%M");
> ss.imbue(std::locale(std::locale::classic(), timefacet));
> ss.exceptions(std::ios_base::failbit); // turn on exceptions
> ss >> t;
> }
> catch(std::exception& e) {
> std::cout << e.what() << std::endl;
> }
> return t;
>
> }
I think the best way would be to support something like
ptime t;
my_istream >> time_format("%d.%m.%Y %H:%M") >> t;
time_format would be defined as something similar to
implemetation_defined time_format(const char *format, const std::locale
&loc = std::locale::classic());
and using io manipulators it will do all the magic
(facet/imbue/whatever) stuff automatically.
This will be much easier to use...
Just a thought,
Yuval
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