I need to be able to transform and compare dates in c++. I found that the boost library pretty much suited my needs but I can´t get it to work correctly:
// include headers...
using namespace boost::posix_time;
using namespace boost::gregorian;
ptime p1(second_clock::universal_time());
p1 -= weeks(5); // Subtract 5 weeks from p1 using boost...
std::string old_date("2011-11-19"); // format:YYYY-MM-DD
std:stringstream ss(old_date);
ptime p2;
p2 << ss;
if(p1 <= p2){
// this should not happen in this case (yet it does!) p1 > p2!!
}
Basically I want to be able to subtract weeks (or months) of a local date and then compare the result with a date given as a string in YYYY-MM-DD format…
Your locale might not be set to recognize dates in the YYYY-MM-DD format. Try setting the input facet format as in the Format Strings example.
Here’s an example that shows how to set the input and output formats of a stringstream to “ISO extended”: