O_Egor

date.cpp

Oct 15th, 2022
698
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.95 KB | None | 0 0
  1. #include "date.h"
  2.  
  3. using namespace std;
  4.  
  5. Date::Date() : _Year(0), _Month(0), _Day(0) {}
  6.  
  7. Date::Date(int year, int month, int day)
  8. {
  9.     if (month < 1 || month > 12)
  10.     {
  11.         string err = "Month value is invalid: " + to_string(month);
  12.         throw logic_error(err);
  13.     }
  14.    
  15.     if (day < 1 || day > 31)
  16.     {
  17.         string err = "Day value is invalid: " + to_string(day);
  18.         throw logic_error(err);
  19.     }
  20.  
  21.     _Year = year;
  22.     _Month = month;
  23.     _Day = day;
  24. }
  25.  
  26. int Date::getYear() const
  27. {
  28.     return _Year;
  29. }
  30.  
  31. int Date::getMonth() const
  32. {
  33.     return _Month;
  34. }
  35.  
  36. int Date::getDay() const
  37. {
  38.     return _Day;
  39. }
  40.  
  41. Date ParseDate(istream& is)
  42. {
  43.     string str_date;
  44.     is >> str_date;
  45.  
  46.     stringstream str_stream_date(str_date);
  47.  
  48.     int year = -1;
  49.     str_stream_date >> year;
  50.  
  51.     if (str_stream_date.fail() || str_stream_date.peek() != '-')
  52.     {
  53.         throw logic_error("Wrong date format: " + str_date);
  54.     }
  55.     str_stream_date.ignore();
  56.  
  57.     int month = -1;
  58.     str_stream_date >> month;
  59.  
  60.     if (str_stream_date.fail() || str_stream_date.peek() != '-')
  61.     {
  62.         throw logic_error("Wrong date format: " + str_date);
  63.     }
  64.     str_stream_date.ignore();
  65.  
  66.     int day = -1;
  67.     str_stream_date >> day;
  68.  
  69.     if (str_stream_date.fail() || !str_stream_date.eof())
  70.     {
  71.         throw logic_error("Wrong date format: " + str_date);
  72.     }
  73.     return { year, month, day };
  74. }
  75.  
  76. string Date::DateToStr() const
  77. {
  78.     ostringstream oss;
  79.     oss << *this;
  80.     return oss.str();
  81. }
  82.  
  83. ostream& operator<<(ostream& os, const Date& _Date)
  84. {
  85.     return os << setw(4) << setfill('0') << _Date.getYear() << '-'
  86.        << setw(2) << setfill('0') << _Date.getMonth() << '-'
  87.        << setw(2) << setfill('0') << _Date.getDay();
  88. }
  89.  
  90. ostream& operator<<(ostream& os, const pair <Date, string>& _Pair)
  91. {
  92.     return os << _Pair.first << "  " << _Pair.second;
  93. }
  94.  
  95. bool operator<(const Date& _Left, const Date& _Right)
  96. {
  97.     return make_tuple(_Left.getYear(), _Left.getMonth(), _Left.getDay()) <
  98.            make_tuple(_Right.getYear(), _Right.getMonth(), _Right.getDay());
  99. }
  100.  
  101. bool operator<=(const Date& _Left, const Date& _Right)
  102. {
  103.     return make_tuple(_Left.getYear(), _Left.getMonth(), _Left.getDay()) <=
  104.         make_tuple(_Right.getYear(), _Right.getMonth(), _Right.getDay());
  105. }
  106.  
  107. bool operator>(const Date& _Left, const Date& _Right)
  108. {
  109.     return make_tuple(_Left.getYear(), _Left.getMonth(), _Left.getDay()) >
  110.         make_tuple(_Right.getYear(), _Right.getMonth(), _Right.getDay());
  111. }
  112.  
  113. bool operator>=(const Date& _Left, const Date& _Right)
  114. {
  115.     return make_tuple(_Left.getYear(), _Left.getMonth(), _Left.getDay()) >=
  116.         make_tuple(_Right.getYear(), _Right.getMonth(), _Right.getDay());
  117. }
  118.  
  119. bool operator==(const Date& _Left, const Date& _Right)
  120. {
  121.     return make_tuple(_Left.getYear(), _Left.getMonth(), _Left.getDay()) ==
  122.         make_tuple(_Right.getYear(), _Right.getMonth(), _Right.getDay());
  123. }
  124.  
  125. bool operator!=(const Date& _Left, const Date& _Right)
  126. {
  127.     return make_tuple(_Left.getYear(), _Left.getMonth(), _Left.getDay()) !=
  128.         make_tuple(_Right.getYear(), _Right.getMonth(), _Right.getDay());
  129. }
Advertisement
Add Comment
Please, Sign In to add comment