Subtracting months/years from boost::posix_time::ptime

Posted by Zack on Stack Overflow See other posts from Stack Overflow or by Zack
Published on 2010-03-21T15:14:57Z Indexed on 2010/03/21 15:21 UTC
Read the original article Hit count: 146

Filed under:
|
|

I have a boost::posix_time::ptime that points to March 31st 2010 like this:

ptime p(date(2010, Mar, 31));

I would like to subtract a month (and possibly years) from this date. From the docs I see these two operators: ptime operator-(time_duration) and ptime operator-(days) but none of them can work with months/years. If I try and do:

time_duration duration = hours(24 * 30);
ptime pp = p - duration;

I'm getting March 1st and if I'm trying:

ptime pp = p - days(30);

I'm still getting March 1st, while I'd like to get February 28th.
How can I achieve my desired result? (I would like to get the desired result also when subtracting a month from March 28, 29, 30)

© Stack Overflow or respective owner

Related posts about boost

Related posts about c++