#include //for cout #include //for class system_clock #include //for the function localtime #include "date.h" using namespace std; const int date::length[] { 0, //dummy, so that January will have subscript 1 31, //January 28, //February. Pretend there are no leap years. 31, //March 30, //April 31, //May 30, //June 31, //July 31, //August 30, //September 31, //October 30, //November 31 //December }; date::date(int m, int d, int y) : day(365 * y + d - 1) { for (int month {1}; month < m; ++month) { day += length[month]; } } date::date() //today's date { const auto now {chrono::system_clock::now()}; const time_t t {chrono::system_clock::to_time_t(now)}; const tm *const p {localtime(&t)}; day = 365 * (p->tm_year + 1900) + p->tm_yday; } ostream& operator<<(ostream& os, const date& d) { const int year {d.day / 365}; int dayOfYear {d.day % 365 + 1}; //1 to 365 inclusive int month {1}; for (; dayOfYear > date::length[month]; ++month) { dayOfYear -= date::length[month]; } os << month << "/" << dayOfYear << "/" << year; return os; } istream& operator>>(istream& is, date& d) { int month {0}; int dayOfMonth {0}; int year {0}; char slash1 {'\0'}; char slash2 {'\0'}; if (!(is >> month >> slash1 >> dayOfMonth >> slash2 >> year)) { return is; } if (slash1 != '/' || slash2 != '/') { is.setstate(ios::failbit); return is; } if (month < 1 || month > 12) { is.setstate(ios::failbit); return is; } if (dayOfMonth < 1 || dayOfMonth > date::length[month]) { is.setstate(ios::failbit); return is; } d = date {month, dayOfMonth, year}; return is; } int operator-(const date& a, const date& b) { return a.day - b.day; } bool operator==(const date& a, const date& b) { return a.day == b.day; } bool operator<(const date& a, const date& b) { return a.day < b.day; }