#pragma once #include #include #include #include "ByteArrayProvider.h" namespace Incart::Common { class DateObject { private: uint32_t m_year; uint8_t m_month; // 1 - 12 uint8_t m_day; // 1 - 31 bool m_isDefault; public: DateObject() : m_year(0) , m_month(1) , m_day(1) , m_isDefault(true) { } DateObject(uint8_t day_, uint8_t month_, uint32_t year_) : m_year(year_) , m_month(getCorrectMonth(month_)) , m_day(getCorrectDay(day_, m_month, m_year)) , m_isDefault(false) { } public: uint32_t getYear() const { return m_year; } void setYear(uint32_t year_) { m_year = year_; if (m_isDefault) { m_isDefault = false; } } uint8_t getMonth() const { return m_month; } void setMonth(uint8_t month_) { m_month = getCorrectMonth(month_); if (m_isDefault) { m_isDefault = false; } } uint8_t getDay() const { return m_day; } void setDay(uint8_t day_) { m_day = getCorrectDay(day_, m_month, m_year); if (m_isDefault) { m_isDefault = false; } } bool isDefault() const { return m_isDefault; } // format: yyyy.MM.dd static DateObject fromString(const std::string& dateText_) { if (dateText_.size() != 10) { return DateObject{}; } std::string yearText = dateText_.substr(0, 4); std::string monthText = dateText_.substr(5, 2); std::string dayText = dateText_.substr(8, 2); uint32_t year = static_cast(std::stoi(yearText)); uint8_t month = static_cast(std::stoi(monthText)); uint8_t day = static_cast(std::stoi(dayText)); return DateObject{day, month, year}; } static bool isLeapYear(uint32_t year_) { return (((year_ % 4) == 0) && ((year_ % 100) == 0)) || (year_ % 400 == 0); } static uint8_t getDayCount(uint8_t month_, uint32_t year_) { switch (month_) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: return 31; case 2: return isLeapYear(year_) ? 29 : 28; case 4: case 6: case 9: case 11: return 30; default: return 0; } } private: static uint8_t getCorrectMonth(uint8_t month_) { return month_ < 1 ? 1 : (month_ > 12 ? 12 : month_); } static uint8_t getCorrectDay(uint8_t day_, uint8_t month_, uint32_t year_) { if (day_ < 1) { return 1; } uint8_t dayCount = getDayCount(month_, year_); if (day_ > dayCount) { return dayCount; } return day_; } }; } // namespace Incart::Common