avr39ripe

cppDateClass

Jul 20th, 2021 (edited)
378
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2.  
  3. class Date
  4. {
  5.     uint16_t year;
  6.     uint8_t month;
  7.     uint8_t day;
  8.    
  9.     uint8_t checkMonth(uint8_t monthP)
  10.     {
  11.         if (monthP < 1) return 1;
  12.         if (monthP > 12) return 12;
  13.         return monthP;
  14.     }
  15.     uint8_t checkDay(uint8_t dayP, uint8_t monthP, uint16_t yearP)
  16.     {
  17.         const uint8_t maxDays{ Date::daysInMonth(monthP, yearP) };
  18.         if (dayP < 1) return 1;
  19.         if (dayP > maxDays) return maxDays;
  20.         return dayP;
  21.     }
  22. public:
  23.     static uint8_t daysInMonth(uint8_t month, uint16_t year)
  24.     {
  25.         return 30 + (((month < 8) and (month % 2 != 0)) or ((month >= 8) and (month % 2 == 0))) + ((month == 2) * (-2) + isLeapYear(year));
  26.     }
  27.  
  28.     static bool isLeapYear(uint16_t year)
  29.     {
  30.         return year % 400 == 0 or year % 4 == 0 and year % 100 != 0;
  31.     }
  32.     static const uint8_t maxMonth{ 12 };
  33.  
  34.     Date() : Date(1, 1, 1970) {}
  35.     Date(uint8_t dayP, uint8_t monthP, uint16_t yearP)
  36.         : year{yearP}, month{ checkMonth(monthP)},day{checkDay(dayP,month,yearP)} {}
  37.  
  38.     Date& setDay(uint8_t dayP) { day = checkDay(dayP, month, year); return *this; }
  39.     Date& setMonth(uint8_t monthP) { month = checkMonth(monthP); day = checkDay(day, month, year); return *this; }
  40.     Date& setYear(uint16_t yearP) { year = yearP; return *this; }
  41.  
  42.     uint8_t getDay() const { return day; }
  43.     uint8_t getMonth() const { return month; }
  44.     uint16_t getYear() const { return year; }
  45.  
  46.     Date& print() { std::cout << +day << '.' << +month << '.' << year << '\n'; return *this; }
  47. };
  48.  
  49.  
  50. int main()
  51. {
  52.     std::cout << std::boolalpha << (Date::isLeapYear(2020) == true) << '\n';
  53.     std::cout << std::boolalpha << (Date::isLeapYear(2021) == false) << '\n';
  54.     std::cout << std::boolalpha << (Date::isLeapYear(1800) == false) << '\n';
  55.     std::cout << std::boolalpha << (Date::isLeapYear(1600) == true) << '\n';
  56.     std::cout << std::boolalpha << (Date::daysInMonth(12, 2021) == 31) << '\n';
  57.     std::cout << std::boolalpha << (Date::daysInMonth(1, 2021) == 31) << '\n';
  58.     std::cout << std::boolalpha << (Date::daysInMonth(4, 2021) == 30) << '\n';
  59.     std::cout << std::boolalpha << (Date::daysInMonth(2, 2021) == 28) << '\n';
  60.     std::cout << std::boolalpha << (Date::daysInMonth(2, 2020) == 29) << '\n';
  61.  
  62.     Date date1;
  63.     std::cout << std::boolalpha << (date1.getDay() == 1) << '\n';
  64.     std::cout << std::boolalpha << (date1.getMonth() == 1) << '\n';
  65.     std::cout << std::boolalpha << (date1.getYear() == 1970) << '\n';
  66.  
  67.     Date date2{ 26,7,1981 };
  68.     std::cout << std::boolalpha << (date2.getDay() == 26) << '\n';
  69.     std::cout << std::boolalpha << (date2.getMonth() == 7) << '\n';
  70.     std::cout << std::boolalpha << (date2.getYear() == 1981) << '\n';
  71.  
  72.     date2.setDay(32);
  73.     std::cout << std::boolalpha << (date2.getDay() == 31) << '\n';
  74.     date2.setDay(0);
  75.     std::cout << std::boolalpha << (date2.getDay() == 1) << '\n';
  76.     date2.setDay(31);
  77.     date2.setMonth(2);
  78.     std::cout << std::boolalpha << (date2.getDay() == 28) << '\n';
  79.     std::cout << std::boolalpha << (date2.getMonth() == 2) << '\n';
  80.  
  81.     return 0;
  82. }
RAW Paste Data