Date & BaseDate::add_month(const int n) { int d = day(); int m = month(); int y = year(); if(n < 0) { // "removing" month's int j = abs(n); // n is negative.. for(int i = 0; i < j; ++i) { --m; if(m == 0) { --y; m = 12; } int daysInMonth = days_this_month(y, m); if(d > daysInMonth) { d = daysInMonth; } } } else { // adding month's for(int i = 0; i < n; ++i){ ++m; // inc month if(m > 12) { ++y; m = 1; } int daysInMonth = days_this_month(y, m); if(d > daysInMonth) { d = daysInMonth; } } } setMjdFromDate(y, m, d); return *this; };
static int print_budget(void) { struct tm *stime; int monthly_budget, daily_budget, spent, budget_today, remaining, next_day; int ret; if((ret = read_db(&monthly_budget, &spent)) != EXIT_SUCCESS) return ret; stime = local_time(); daily_budget = monthly_budget / days_this_month(); budget_today = monthly_budget * stime->tm_mday / days_this_month(); remaining = budget_today - spent; if(remaining >= 0) { printf("%02d.%02d.%04d; %d;\n", stime->tm_mday, stime->tm_mon + 1, stime->tm_year + 1900, remaining); } else if(spent < monthly_budget) { next_day = days_this_month() - (monthly_budget - spent) / daily_budget; if(spent % daily_budget == 0) next_day++; printf("%02d.%02d.%4d;\n", next_day, stime->tm_mon + 1, stime->tm_year + 1900); } else { if(stime->tm_mon++ > 11) { stime->tm_mon = 0; stime->tm_year++; } printf("01.%02d.%4d;\n", stime->tm_mon + 1, stime->tm_year + 1900); } return EXIT_SUCCESS; }
void Date::add_month(int n) { int yearShift = (int) floor( (double) (((int) _month) + n -1)/ ((int) month_this_year())); //if(n < 0){yearShift--;} add_year(yearShift); _month = ((12+(((int)_month-1+ n ) % 12)) % 12) +1; //Updating year according to removed/added number of month if(_day>days_this_month()){ _day = days_this_month(); } }
void Date::add_year(int n) { /*if( (_year+n > maxYear) || (_year+n < minYear)){ std::cout << "Warning or something might be unspported\n"; }*/ _year += n; if(_month > month_this_year()){ _month = month_this_year(); } if(_day > days_this_month()){ _day = days_this_month(); } }
void Date::add_year (int n) { this->y += n; if(!valid_date(year(), month(), day())) { this->d = days_this_month(); } this->julian_day_number = calc_julian_day_number(year(), month(), day()); }
void Date::add_month (int n) { // Negative if(n < 0) { int tmp = month(); this->m = tmp - abs(n) > 0 ? tmp - abs(n) : 12 - abs(n + tmp) % 12; if(n + tmp <= 0) { this->y -= (1 + floor(abs(n + tmp)/12)); } } else { int months = abs(n) + month(); if(months % 12 != 0) this->m = months % 12; else this->m = 12; if(months > 12 && months % 12 != 0) { this->y += months / 12; } else if (months > 12) { this->y += months / 12 - 1; } } if(!valid_date(year(), month(), day())) { this->d = days_this_month(); } this->julian_day_number = calc_julian_day_number(year(), month(), day()); }
void lab2::Julian::add_month(int n) { const int years = n / 12; const int months = n % 12; const int new_month = _month + months; _year += years; if (new_month > 12) { _month = new_month - 12; ++_year; } else if (new_month < 1) { _month = new_month + 12; --_year; } else { _month += months; } // Year 0 is -1 in Julian if ((_year == 0) && (n > 0)) _year = 1; else if ((_year == 0) && (n < 0)) _year = -1; const unsigned int dtm = days_this_month(); if (_day > dtm) _day = dtm; date_to_jdn(); }
void Julian::add_one_month(){ if((day()==31&&month()==7) || (day()==31&&month()==12)){ offset+=31; return; } if(day()==31){ offset+=30; return; } if(day()==30&&month()==1){ offset+=30; return; } if(day()==29&&month()==1){ if(is_leap_year()){ offset+=31; }else{ offset+=30; } return; } offset+=days_this_month(); return; }
Date &Date::operator+=(int d) { int tmpDay = _day +d; while((tmpDay < 1)||(tmpDay > (int) days_this_month()) ){ if(tmpDay < 1){ this->add_month((-1)); tmpDay += days_this_month(); } else if( tmpDay> (int) days_this_month()){ tmpDay -= days_this_month(); this->add_month(1); } } _day =(unsigned) tmpDay; return *this; }
bool Date::valid_date(int year, int month, int day) const { if(year <= 1858 || year >= 2558 || month <= 0 || month > months_per_year() || day <= 0 || day > days_this_month()) { return false; } return true; }
void lab2::Gregorian::add_year(int n) { _year += n; const unsigned int dtm = days_this_month(); if (_day > dtm) _day = dtm; date_to_jdn(); }
static int print_budget_verbose(int summary) { struct tm *stime; int monthly_budget, daily_budget, spent, budget_today, remaining, next_day; int ret; if((ret = read_db(&monthly_budget, &spent)) != EXIT_SUCCESS) return ret; stime = local_time(); daily_budget = monthly_budget / days_this_month(); budget_today = monthly_budget * stime->tm_mday / days_this_month(); remaining = budget_today - spent; if(summary) { printf("--------------------------------------\n"); printf(" JUNK FOOD SUMMARY:\n"); printf("--------------------------------------\n"); printf(" Today is %02d.%02d.%4d.\n", stime->tm_mday, stime->tm_mon + 1, stime->tm_year + 1900); printf(" Your monthly budget is: %d.%02d " CURRENCY "\n", BREAKUP(monthly_budget)); printf(" You spent: %d.%02d " CURRENCY "\n", BREAKUP(spent)); printf(" Budget until today: %d.%02d " CURRENCY "\n", BREAKUP(budget_today)); printf("--------------------------------------\n\n"); } printf("You can spend "); if(remaining >= 0) { printf("%d.%02d " CURRENCY "\n\n", BREAKUP(remaining)); } else if(spent < monthly_budget) { next_day = days_this_month() - (monthly_budget - spent) / daily_budget; if(spent % daily_budget == 0) next_day++; printf("again on %02d.%02d.%4d\n\n", next_day, stime->tm_mon + 1, stime->tm_year + 1900); } else { printf("again next month.\n\n"); } return EXIT_SUCCESS; }
lab2::Gregorian::Gregorian(unsigned int year, unsigned int month, unsigned int day) { _year = year; if(month > month_this_year() || month < 1){ throw std::invalid_argument(""); } _month = month; if(day > days_this_month() || day < 1){ throw std::invalid_argument(""); } _day = day; }
void lab2::Julian::add_year(int n) { _year += n; // Year 0 is -1 in Julian if ((_year == 0) && (n > 0)) _year = 1; else if ((_year == 0) && (n < 0)) _year = -1; const unsigned int dtm = days_this_month(); if (_day > dtm) _day = dtm; date_to_jdn(); }
// Check if the set date is valid. void BaseDate::isDateValid() const { int d = day(); int m = month(); int y = year(); if(y < 1858 || y > 2558) { std::out_of_range("Year is not vaild"); } if(m < 1 || m > 12) { std::out_of_range("Month is not valid"); } if (d < 1 || d > days_this_month()) { std::out_of_range("Day is not valid"); } };
Middle& Middle::add_month(int n) { int sign = -1; if( n > 0) { sign = 1; } n = std::abs(n); for(int i = 0; i < n; ++i) { if(days_month(year(), month() + sign) < day()){ _numeric += 30 * sign; } else if(sign > 0) { _numeric += days_this_month(); } else { _numeric -= days_month(year(), month()-1); } } return *this; }
void lab2::Gregorian::add_month(int n) { const int years = n / 12; const int months = n % 12; const int new_month = _month + months; _year += years; if (new_month > 12) { _month = new_month - 12; ++_year; } else if (new_month < 1) { _month = new_month + 12; --_year; } else { _month += months; } const unsigned int dtm = days_this_month(); if (_day > dtm) _day = dtm; date_to_jdn(); }
Gregorian::Gregorian(int new_year,int new_month,int new_day):Date(new_year,new_month,new_day){ if(new_month > 12 || new_month < 1 || new_day < 1 || new_day > days_this_month()){ throw std::out_of_range("Invalid date\n"); } }
static int init_month(const int daily_budget) { return write_db(daily_budget * days_this_month(), 0); }