Boost logo

Boost Users :

Subject: Re: [Boost-users] [date_time] How to parse "5th" into day of month?
From: Adam Nielsen (adam.nielsen_at_[hidden])
Date: 2010-07-04 23:36:12


Just to follow up on this, I've come up with a rather dodgy workaround
for this:

> Here is my code:
>
> std::string strDate = "Fri 5th March 2010, 05:01pm";
> std::string format("%a %e.. %B %Y");

The line above becomes this, which is used to select a different date
format depending on whether the day is a single digit or double digit:

std::string format;
if ((strDate[5] < '0') || (strDate[5] > '9')) {
         // Single-digit day
         format = "%a%e.. %B %Y";
} else {
         // Double-digit day
         format = "%a %d.. %B %Y";
}

> boost::date_time::format_date_parser<boost::gregorian::date, char>
> parser(format, std::locale("C"));
> boost::date_time::special_values_parser<boost::gregorian::date, char>
> svp;
> boost::gregorian::date d =
> parser.parse_date(strDate, format, svp);
>
> std::cout << "Converted date from \"" << strDate
> << "\" to " << d << std::endl;

Seems somewhat of an odd omission that a library with such a strong
emphasis on reading dates from strings would be unable to handle numbers
of different lengths. I'm sure I'm missing something but I still can't
find anything that explains how to achieve this elegantly!

Cheers,
Adam.


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