// // This is example code from Chapter 9.8 "The Date class" of // "Programming -- Principles and Practice Using C++" by Bjarne Stroustrup // #include "Chrono.h" namespace Chrono { // member function definitions: //------------------------------------------------------------------------------ Date::Date(int yy, Month mm, int dd) : y(yy), m(mm), d(dd) { if (!is_date(yy,mm,dd)) throw Invalid(); } //------------------------------------------------------------------------------ const Date& default_date() { static const Date dd(2001,Month::jan,1); // start of 21st century return dd; } //------------------------------------------------------------------------------ Date::Date() :y(default_date().year()), m(default_date().month()), d(default_date().day()) { } //------------------------------------------------------------------------------ void Date:: add_day(int n) { // ... } //------------------------------------------------------------------------------ void Date::add_month(int n) { // ... } //------------------------------------------------------------------------------ void Date::add_year(int n) { if (m==Month::feb && d==29 && !leapyear(y+n)) { // beware of leap years! m = Month::mar; // use March 1 instead of February 29 d = 1; } y+=n; } //------------------------------------------------------------------------------ // helper functions: bool is_date(int y, Month m, int d) { // assume that y is valid if (d<=0) return false; // d must be positive int days_in_month = 31; // most months have 31 days switch (m) { case Month::feb: // the length of February varies days_in_month = (leapyear(y))?29:28; break; case Month::apr: case Month::jun: case Month::sep: case Month::nov: days_in_month = 30; // the rest have 30 days break; } if (days_in_month>(istream& is, Date& dd) { int y, m, d; char ch1, ch2, ch3, ch4; is >> ch1 >> y >> ch2 >> m >> ch3 >> d >> ch4; if (!is) return is; if (ch1!='(' || ch2!=',' || ch3!=',' || ch4!=')') { // oops: format error is.clear(ios_base::failbit); // set the fail bit return is; } dd = Date(y,Month(m),d); // update dd return is; } //------------------------------------------------------------------------------ enum Day { sunday, monday, tuesday, wednesday, thursday, friday, saturday }; //------------------------------------------------------------------------------ Day day_of_week(const Date& d) { // ... return sunday; } //------------------------------------------------------------------------------ Date next_Sunday(const Date& d) { // ... return d; } //------------------------------------------------------------------------------ Date next_weekday(const Date& d) { // ... return d; } //------------------------------------------------------------------------------ } // Chrono //------------------------------------------------------------------------------ int main() try { Chrono::Date holiday(1978, Chrono::Month::jul, 4); // initialization Chrono::Date d2 = Chrono::next_Sunday(holiday); Chrono::Day d = day_of_week(d2); cout << "holiday is " << holiday << " d2 is " << d2 << endl; return holiday != d2; } catch (Chrono::Date::Invalid&) { cerr << "error: Invalid date\n"; return 1; } catch (...) { cerr << "Oops: unknown exception!\n"; return 2; } //------------------------------------------------------------------------------