74 lines
2.2 KiB
C
74 lines
2.2 KiB
C
|
#ifndef DATETIME_H
|
|||
|
#define DATETIME_H
|
|||
|
|
|||
|
#include <chrono>
|
|||
|
#include <ctime>
|
|||
|
#include <stdint.h>
|
|||
|
#include <string_view>
|
|||
|
|
|||
|
class DateTime {
|
|||
|
public:
|
|||
|
enum Week {
|
|||
|
Sunday = 0,
|
|||
|
Monday,
|
|||
|
Tuesday,
|
|||
|
Wednesday,
|
|||
|
Thursday,
|
|||
|
Friday,
|
|||
|
Saturday,
|
|||
|
};
|
|||
|
DateTime(const std::chrono::time_point<std::chrono::system_clock> &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<std::chrono::system_clock> 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<std::chrono::system_clock> &time,
|
|||
|
const std::string_view &format = "%F %T");
|
|||
|
static std::chrono::time_point<std::chrono::system_clock> 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<uint8_t,uint8_t,uint8_t>
|
|||
|
*/
|
|||
|
static std::tuple<uint8_t, uint8_t, uint8_t> 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
|