void Date::validate(int day, int month, int year) { int chek; int LeapYear; if ( month > 12 || month < 1) { throw InvalidDate ("invalid month"); } LeapYear = this->year % 4; chek = (this->month % 2) ^ (this->month / 8); if ( chek == 1 ) { if (this->day > 31) { throw InvalidDate ("invalid day"); }; } else if ( this->month == 2 ) { if ( LeapYear == 0) { if (this->day > 28) { throw InvalidDate("invalid day"); } } else { if (this->day > 29) { throw InvalidDate("invalid day"); } } } else { if (this->day > 30) { throw InvalidDate("invalid day"); } } }
void greg2jul(unsigned& jd, int y, int m, int d) { if( ! Date::isValid(y, m, d) ) { throw InvalidDate(); } jd=(1461*(y+4800+(m-14)/12))/4+(367*(m-2-12*((m-14)/12)))/12-(3*((y+4900+(m-14)/12)/100))/4+d-32075; }
Date::Date(const std::string& str, const std::string& fmt) { unsigned year = 0; unsigned month = 1; unsigned day = 1; enum { state_0, state_fmt, state_two } state = state_0; try { std::string::const_iterator dit = str.begin(); std::string::const_iterator it; for (it = fmt.begin(); it != fmt.end() && dit != str.end(); ++it) { char ch = *it; switch (state) { case state_0: if (ch == '%') state = state_fmt; else { if (ch == '*') skipNonDigit(dit, str.end()); else if (*dit != ch && ch != '?') throw InvalidDate("string <" + str + "> does not match date format <" + fmt + '>'); else ++dit; } break; case state_fmt: if (*it != '%') state = state_0; switch (ch) { case 'Y': year = getInt(dit, str.end(), 4); break; case 'y': year = getInt(dit, str.end(), 2); year += (year < 50 ? 2000 : 1900); break; case 'm': month = getUnsigned(dit, str.end(), 2); break; case 'd': day = getUnsigned(dit, str.end(), 2); break; case '2': state = state_two; break; default: throw InvalidDate("invalid date format <" + fmt + '>'); } break; case state_two: state = state_0; switch (ch) { case 'm': month = getUnsignedF(dit, str.end(), 2); break; case 'd': day = getUnsignedF(dit, str.end(), 2); break; default: throw InvalidDate("invalid date format <" + fmt + '>'); } } } if (it != fmt.end() || dit != str.end()) throw InvalidDate("string <" + str + "> does not match date format <" + fmt + '>'); set(year, month, day); } catch (const std::invalid_argument&) { throw InvalidDate("string <" + str + "> does not match date format <" + fmt + '>'); } }
void Book::setDate(Date d) { if (!d.isValid()) throw InvalidDate(); else dateAdded = d; }