#ifndef DATETIME_H #define DATETIME_H #include #include #include #include class DateTime { public: enum Week { Sunday = 0, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, }; DateTime(const std::chrono::time_point &time); DateTime(size_t year, size_t month, size_t day, size_t hour, size_t minute, size_t second); size_t year() const; size_t month() const; size_t day() const; size_t hour() const; void setHour(size_t hour); size_t minute() const; void setMinute(size_t minute); size_t second() const; void setSecond(size_t second); Week week() const; std::chrono::time_point operator()(); std::string toString(const std::string_view &format = "%F %T") const; DateTime tomorrow(); // Returns the number of milliseconds since 1970-01-01T00:00:00 Universal Coordinated Time. This number is like the // POSIX time_t variable, but expressed in milliseconds instead. static int64_t currentMSecsSinceEpoch(); static int64_t currentSecsSinceEpoch(); static DateTime currentDateTime(); static std::string toString(const std::chrono::time_point &time, const std::string_view &format = "%F %T"); static std::chrono::time_point makeDateTime(size_t year, size_t month, size_t day, size_t hour, size_t minute, size_t second); static bool isLeapYear(size_t year); static size_t days(size_t year, size_t month); /** * @brief parse time like "12:34:56"、"23:56" * * @return std::tuple */ static std::tuple parseTime(const std::string &text); protected: static std::time_t makeTime(size_t year, size_t month, size_t day, size_t hour, size_t minute, size_t second); private: size_t m_year{1990}; size_t m_month{1}; size_t m_day{0}; size_t m_hour{0}; size_t m_minute{0}; size_t m_second{0}; Week m_week; }; #endif // DATETIME_H