Kylin/Universal/DateTime.h

85 lines
2.4 KiB
C
Raw Normal View History

2023-07-21 11:53:19 +08:00
#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()();
2024-07-15 17:51:54 +08:00
/**
* @brief toString
* @param format %Y%m%d%H%M%S 20240715171145
* @return
*/
2023-07-21 11:53:19 +08:00
std::string toString(const std::string_view &format = "%F %T") const;
2024-01-06 00:06:35 +08:00
/**
* @brief 2023-10-12 12:10:23 then return 2023-10-13 12:10:23
*
* @return DateTime
*/
2023-07-21 11:53:19 +08:00
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