Domanda

The boost::gregorian::from_*string() parsing functions in Boost seem to only handle 4-digit years (causing a run-time error for 2-digit years).

What's the cleanest way to use boost::gregorian::from_*string() functions to process 2-digit years?

One possibility is to do some pre-procesisng on the date strings in conjunction with programming rules for adding 2000 or 1900 to sanitize the date format, but I don't want to reinvent the wheel and add too much code if there's a better way to do this.

È stato utile?

Soluzione

It doesn't look like this format is supported in boost. Fortunately, parsing date format is a one-liner. Below is a parser written in AXE, and I'm sure you can write similar code in Spirit.

using namespace boost::gregorian;
using namespace axe::shortcuts;
unsigned year, month, day;
auto date_rule = _uint >> year > '/' > _uint >> month > '/' > _uint >> day > _z;
std::string date_string("99/01/22");
if(date_rule(date_string.begin(), date_string.end()).matched)
{
   date d(year < 100 ? year + 1900 : year, month, day);
   // etc.
}

Altri suggerimenti

Boost.DateTime cannot do that – the docs say that there is a %C format flag that matches the behavior you want, but it can only be used for output, not input, and even then only on a limited set of platforms.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top