Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "date.h"
- using namespace std;
- Date::Date() : _Year(0), _Month(0), _Day(0) {}
- Date::Date(int year, int month, int day)
- {
- if (month < 1 || month > 12)
- {
- string err = "Month value is invalid: " + to_string(month);
- throw logic_error(err);
- }
- if (day < 1 || day > 31)
- {
- string err = "Day value is invalid: " + to_string(day);
- throw logic_error(err);
- }
- _Year = year;
- _Month = month;
- _Day = day;
- }
- int Date::getYear() const
- {
- return _Year;
- }
- int Date::getMonth() const
- {
- return _Month;
- }
- int Date::getDay() const
- {
- return _Day;
- }
- Date ParseDate(istream& is)
- {
- string str_date;
- is >> str_date;
- stringstream str_stream_date(str_date);
- int year = -1;
- str_stream_date >> year;
- if (str_stream_date.fail() || str_stream_date.peek() != '-')
- {
- throw logic_error("Wrong date format: " + str_date);
- }
- str_stream_date.ignore();
- int month = -1;
- str_stream_date >> month;
- if (str_stream_date.fail() || str_stream_date.peek() != '-')
- {
- throw logic_error("Wrong date format: " + str_date);
- }
- str_stream_date.ignore();
- int day = -1;
- str_stream_date >> day;
- if (str_stream_date.fail() || !str_stream_date.eof())
- {
- throw logic_error("Wrong date format: " + str_date);
- }
- return { year, month, day };
- }
- string Date::DateToStr() const
- {
- ostringstream oss;
- oss << *this;
- return oss.str();
- }
- ostream& operator<<(ostream& os, const Date& _Date)
- {
- return os << setw(4) << setfill('0') << _Date.getYear() << '-'
- << setw(2) << setfill('0') << _Date.getMonth() << '-'
- << setw(2) << setfill('0') << _Date.getDay();
- }
- ostream& operator<<(ostream& os, const pair <Date, string>& _Pair)
- {
- return os << _Pair.first << " " << _Pair.second;
- }
- bool operator<(const Date& _Left, const Date& _Right)
- {
- return make_tuple(_Left.getYear(), _Left.getMonth(), _Left.getDay()) <
- make_tuple(_Right.getYear(), _Right.getMonth(), _Right.getDay());
- }
- bool operator<=(const Date& _Left, const Date& _Right)
- {
- return make_tuple(_Left.getYear(), _Left.getMonth(), _Left.getDay()) <=
- make_tuple(_Right.getYear(), _Right.getMonth(), _Right.getDay());
- }
- bool operator>(const Date& _Left, const Date& _Right)
- {
- return make_tuple(_Left.getYear(), _Left.getMonth(), _Left.getDay()) >
- make_tuple(_Right.getYear(), _Right.getMonth(), _Right.getDay());
- }
- bool operator>=(const Date& _Left, const Date& _Right)
- {
- return make_tuple(_Left.getYear(), _Left.getMonth(), _Left.getDay()) >=
- make_tuple(_Right.getYear(), _Right.getMonth(), _Right.getDay());
- }
- bool operator==(const Date& _Left, const Date& _Right)
- {
- return make_tuple(_Left.getYear(), _Left.getMonth(), _Left.getDay()) ==
- make_tuple(_Right.getYear(), _Right.getMonth(), _Right.getDay());
- }
- bool operator!=(const Date& _Left, const Date& _Right)
- {
- return make_tuple(_Left.getYear(), _Left.getMonth(), _Left.getDay()) !=
- make_tuple(_Right.getYear(), _Right.getMonth(), _Right.getDay());
- }
Advertisement
Add Comment
Please, Sign In to add comment