src/Date.cpp
/*!
    Returns a Date object containing a date \a nmonths later than the
    date of this object (or earlier if \a nmonths is negative).

    \note If the ending day/month combination does not exist in the
    resulting month/year, this function will return a date that is the
    latest valid date.

    \sa addDays(), addYears()
*/
Returns a Date object containing a date \a nmonths later than the
date of this object (or earlier if \a nmonths is negative).

\note If the ending day/month combination does not exist in the
resulting month/year, this function will return a date that is the
latest valid date.

\sa addDays(), addYears()
Returns a Date object containing a date \ a nmonths later than the date of this object ( or earlier if \ a nmonths is negative ) . \ note If the ending day / month combination does not exist in the resulting month / year this function will return a date that is the latest valid date . \ sa addDays () addYears ()
Date Date::addMonths(int months) const
{
    if (!isValid())
        return Date();
    if (!months)
        return *this;
    int old_y = m_dt.year;
    int y = m_dt.year;
    int m = m_dt.mon;
    int d = m_dt.mday;
    bool increasing = months > 0;
    while (months != 0) {
        if (months < 0 && months + 12 <= 0) {
            y--;
            months += 12;
        }
        else if (months < 0) {
            m += months;
            months = 0;
            if (m <= 0) {
                --y;
                m += 12;
            }
        }
        else if (months - 12 >= 0) {
            y++;
            months -= 12;
        }
        else if (m == 12) {
            y++;
            m = 0;
        }
        else {
            m += months;
            months = 0;
            if (m > 12) {
                ++y;
                m -= 12;
            }
        }
    }
    if ((old_y > 0 && y <= 0) ||
        (old_y < 0 && y >= 0))
        y += increasing ? +1 : -1;
    return fixedDate(y, m, d);
}
Date Date :: addMonths ( int months ) const { if ( ! isValid ( ) ) return Date ( ) ; if ( ! months ) return * this ; int old_y = m_dt . year ; int y = m_dt . year ; int m = m_dt . mon ; int d = m_dt . mday ; bool increasing = months > 0 ; while ( months != 0 ) { if ( months < 0 && months + 12 <= 0 ) { y -- ; months += 12 ; } else if ( months < 0 ) { m += months ; months = 0 ; if ( m <= 0 ) { -- y ; m += 12 ; } } else if ( months - 12 >= 0 ) { y ++ ; months -= 12 ; } else if ( m == 12 ) { y ++ ; m = 0 ; } else { m += months ; months = 0 ; if ( m > 12 ) { ++ y ; m -= 12 ; } } } if ( ( old_y > 0 && y <= 0 ) || ( old_y < 0 && y >= 0 ) ) y += increasing ? +1 : -1 ; return fixedDate ( y , m , d ) ; }
Returns a Date object containing a date \a nmonths later than the
date of this object (or earlier if \a nmonths is negative).
Returns a Date object containing a date \ a nmonths later than the date of this object ( or earlier if \ a nmonths is negative ) .
// was there a sign change? // yes, adjust the date by +1 or -1 years