Date& Date::addDays(int n) { if(n>0) { if(n<=getDaysLeftOfMonth()) { d+=n; } else { n-=getDaysLeftOfMonth()+1; addMonths(1); d=1; addDays(n); } } else { if(abs(n)<getDaysPassedOfMonth()) { d+=n; } else { n+=getDaysPassedOfMonth(); addMonths(-1); d=getDaysOfMonth(); addDays(n); } } return *this; }
void Date::addTime(std::string time, std::string unit) { Split splt; // default for units: inherent units if( unit.size() == 0 ) unit = unitStr; // It is save to append units (even if empty) to time // Note: time-unit overrules parameter unit. time += unit; // Split string at positions where digits and characters alternate. splt.setSeparator(":alnum:"); splt = time ; // If there is a mix of digits and non-digits, splt.size() >= 2: // isNonDigit && size == 1 is an error. // size == 0 is an error. // But, for !isNonDigit && size == 1 we got the default bool isNon = ! hdhC::isDigit(time); if( (splt.size() == 1 && isNon) || (splt.size() == 0) ) { std::ostringstream ostr(std::ios::app); ostr << "Date::addTime()" << "\nerror in 2nd parameter (time=" << time << ")" ; exceptionError( ostr.str() ); } // unitStr and int strings are empty. // Pure digits are always converted to days. if( splt.size() == 1 ) { addDays( splt.toDouble(0) ) ; return; } std::string str(splt[1]); if( str[0] == 'y' ) addYears( splt.toDouble(0) ); else if( str.substr(0,2) == "mo" ) addMonths( splt.toDouble(0) ); else if( str[0] == 'd' ) addDays( splt.toDouble(0) ); else if( str[0] == 'h' ) addHours( splt.toDouble(0) ); else if( str.substr(0,2) == "mi" ) addMinutes( splt.toDouble(0) ); else if( str[0] == 's' ) addSeconds( splt.toDouble(0) ); return; }
void Date::addTime(double time, std::string unit) { if( unit.size() == 0 ) unit = unitStr; if( unit[0] == 'y' ) addYears( time ); else if( unit.substr(0,2) == "mo" ) addMonths( time ); else if( unit[0] == 'd' ) addDays( time ); else if( unit[0] == 'h' ) addHours( time ); else if( unit.substr(0,2) == "mi" ) addMinutes( time ); else if( unit[0] == 's' ) addSeconds( time ); else // the default addDays( time ); return; }