#include #include #include #include #include "date.h" using namespace std; Date::Date(int y, int m, int d) : year(y), month(m), day(d) { if ( month < 1 || month > 12 ) { throw std::domain_error{"Month " + to_string(m) + " doesn't exist!"}; } if ( day < 1 || day > days_in_month() ) { throw std::domain_error{"Day " + to_string(day) + " invalid"}; } } bool Date::is_leap_year() const { if ( year % 400 == 0 ) return true; if ( year % 100 == 0 ) return false; return year % 4 == 0; } int Date::days_in_month() const { static constexpr const array days {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; if (month < 1 || month > 12) return 0; if ( month == 2 && is_leap_year() ) return 29; return days.at(month); } void Date::next_date() { ++day; if ( day > days_in_month() ) { day = 1; ++month; if (month > 12) { month = 1; ++year; } } } ostream& operator<<(ostream & os, Date const& d) { return os << setfill('0') << setw(4) << d.year << "-" << setw(2) << d.month << "-" << setw(2) << d.day << setfill(' '); }