|
| 1 | +#ifndef _FRPC_DATE_TIME_H_ |
| 2 | +#define _FRPC_DATE_TIME_H_ |
| 3 | + |
| 4 | +#include <chrono> |
| 5 | +#include <cstdint> |
| 6 | + |
| 7 | +namespace frpc { |
| 8 | + |
| 9 | +struct Date { |
| 10 | + Date() = default; |
| 11 | + Date(uint16_t year, uint8_t month, uint8_t day) |
| 12 | + : year(year) |
| 13 | + , month(month) |
| 14 | + , day(day) { |
| 15 | + } |
| 16 | + |
| 17 | + uint16_t year{}; |
| 18 | + uint8_t month{}; |
| 19 | + uint8_t day{}; |
| 20 | + |
| 21 | + MSGPACK_DEFINE(year, month, day) |
| 22 | +}; |
| 23 | + |
| 24 | +NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Date, year, month, day) |
| 25 | + |
| 26 | +inline std::string toString(const Date& value) { |
| 27 | + std::ostringstream ss; |
| 28 | + ss << "Date" |
| 29 | + << " year=" << value.year << " month=" << static_cast<uint32_t>(value.month) |
| 30 | + << " day=" << static_cast<uint32_t>(value.day); |
| 31 | + return ss.str(); |
| 32 | +} |
| 33 | + |
| 34 | +inline bool operator==(const Date& lhs, const Date& rhs) { |
| 35 | + return (lhs.year == rhs.year) && (lhs.month == rhs.month) && (lhs.day == rhs.day); |
| 36 | +} |
| 37 | + |
| 38 | +inline bool operator!=(const Date& lhs, const Date& rhs) { |
| 39 | + return !(lhs == rhs); |
| 40 | +} |
| 41 | + |
| 42 | +struct Time { |
| 43 | + Time() = default; |
| 44 | + Time(uint8_t hour, uint8_t minute, uint8_t second, uint32_t microsecond) |
| 45 | + : hour(hour) |
| 46 | + , minute(minute) |
| 47 | + , second(second) |
| 48 | + , microsecond(microsecond) { |
| 49 | + } |
| 50 | + |
| 51 | + uint8_t hour{}; |
| 52 | + uint8_t minute{}; |
| 53 | + uint8_t second{}; |
| 54 | + uint32_t microsecond{}; |
| 55 | + |
| 56 | + MSGPACK_DEFINE(hour, minute, second, microsecond) |
| 57 | +}; |
| 58 | + |
| 59 | +NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Time, hour, minute, second, microsecond) |
| 60 | + |
| 61 | +inline std::string toString(const Time& value) { |
| 62 | + std::ostringstream ss; |
| 63 | + ss << "Time" |
| 64 | + << " hour=" << static_cast<uint32_t>(value.hour) << " minute=" << static_cast<uint32_t>(value.minute) |
| 65 | + << " second=" << static_cast<uint32_t>(value.second) << " microsecond=" << value.microsecond; |
| 66 | + return ss.str(); |
| 67 | +} |
| 68 | + |
| 69 | +inline bool operator==(const Time& lhs, const Time& rhs) { |
| 70 | + return (lhs.hour == rhs.hour) && |
| 71 | + (lhs.minute == rhs.minute) && |
| 72 | + (lhs.second == rhs.second) && |
| 73 | + (lhs.microsecond == rhs.microsecond); |
| 74 | +} |
| 75 | + |
| 76 | +inline bool operator!=(const Time& lhs, const Time& rhs) { |
| 77 | + return !(lhs == rhs); |
| 78 | +} |
| 79 | + |
| 80 | +struct DateTime { |
| 81 | + DateTime() = default; |
| 82 | + DateTime(const Date& date, const Time& time) |
| 83 | + : date(date) |
| 84 | + , time(time) { |
| 85 | + } |
| 86 | + |
| 87 | + Date date{}; |
| 88 | + Time time{}; |
| 89 | + |
| 90 | + MSGPACK_DEFINE(date, time) |
| 91 | +}; |
| 92 | + |
| 93 | +NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(DateTime, date, time) |
| 94 | + |
| 95 | +inline std::string toString(const DateTime& value) { |
| 96 | + std::ostringstream ss; |
| 97 | + ss << "DateTime" |
| 98 | + << " date=" << toString(value.date) << " time=" << toString(value.time); |
| 99 | + return ss.str(); |
| 100 | +} |
| 101 | + |
| 102 | +inline bool operator==(const DateTime& lhs, const DateTime& rhs) { |
| 103 | + return (lhs.date == rhs.date) && (lhs.time == rhs.time); |
| 104 | +} |
| 105 | + |
| 106 | +inline bool operator!=(const DateTime& lhs, const DateTime& rhs) { |
| 107 | + return !(lhs == rhs); |
| 108 | +} |
| 109 | + |
| 110 | +inline DateTime getFrpcDateTime(std::chrono::system_clock::time_point now_time = std::chrono::system_clock::now()) { |
| 111 | + auto mic = std::chrono::duration_cast<std::chrono::microseconds>(now_time.time_since_epoch()); |
| 112 | + auto microseconds = mic.count() % 1000000; |
| 113 | + auto time_tt = std::chrono::system_clock::to_time_t(now_time); |
| 114 | + struct tm work {}; |
| 115 | + localtime_r(&time_tt, &work); |
| 116 | + Date d(1900 + work.tm_year, work.tm_mon + 1, work.tm_mday); |
| 117 | + Time t(work.tm_hour, work.tm_min, work.tm_sec, microseconds); |
| 118 | + return DateTime{d, t}; |
| 119 | +} |
| 120 | + |
| 121 | +inline std::chrono::system_clock::time_point frpcDateTimeToTimePoint(const DateTime& date_time) { |
| 122 | + struct tm timeinfo {}; |
| 123 | + timeinfo.tm_year = date_time.date.year - 1900; |
| 124 | + timeinfo.tm_mon = date_time.date.month - 1; |
| 125 | + timeinfo.tm_mday = date_time.date.day; |
| 126 | + timeinfo.tm_hour = date_time.time.hour; |
| 127 | + timeinfo.tm_min = date_time.time.minute; |
| 128 | + timeinfo.tm_sec = date_time.time.second; |
| 129 | + auto time_c = std::mktime(&timeinfo); |
| 130 | + return std::chrono::system_clock::from_time_t(time_c) + std::chrono::microseconds{date_time.time.microsecond}; |
| 131 | +} |
| 132 | + |
| 133 | +template <bool millisec = false> |
| 134 | +inline std::string fromFrpcDateTime(const DateTime& date_time, const std::string& format = "%Y-%m-%d %H:%M:%S") { |
| 135 | + struct tm timeinfo {}; |
| 136 | + timeinfo.tm_year = date_time.date.year - 1900; |
| 137 | + timeinfo.tm_mon = date_time.date.month - 1; |
| 138 | + timeinfo.tm_mday = date_time.date.day; |
| 139 | + timeinfo.tm_hour = date_time.time.hour; |
| 140 | + timeinfo.tm_min = date_time.time.minute; |
| 141 | + timeinfo.tm_sec = date_time.time.second; |
| 142 | + std::stringstream ss; |
| 143 | + ss << std::put_time(&timeinfo, format.c_str()); |
| 144 | + if constexpr (millisec) { |
| 145 | + auto millisecond = date_time.time.microsecond / 1000; |
| 146 | + ss << '.' << std::setw(3) << std::setfill('0') << millisecond; |
| 147 | + } |
| 148 | + return ss.str(); |
| 149 | +} |
| 150 | + |
| 151 | +inline DateTime toFrpcDateTime(const std::string& date, const std::string& format = "%Y-%m-%d %H:%M:%S.") { |
| 152 | + struct tm tm {}; |
| 153 | + std::stringstream ss(date); |
| 154 | + ss >> std::get_time(&tm, format.c_str()); |
| 155 | + int milliseconds = 0; |
| 156 | + ss >> milliseconds; |
| 157 | + Date d(1900 + tm.tm_year, tm.tm_mon + 1, tm.tm_mday); |
| 158 | + Time t(tm.tm_hour, tm.tm_min, tm.tm_sec, milliseconds * 1000); |
| 159 | + return DateTime{d, t}; |
| 160 | +} |
| 161 | + |
| 162 | +} // namespace frpc |
| 163 | + |
| 164 | +namespace std { |
| 165 | + |
| 166 | +template <> |
| 167 | +struct hash<frpc::Date> { |
| 168 | + size_t operator()(const frpc::Date& value) const { |
| 169 | + return std::hash<uint16_t>{}(value.year) ^ std::hash<uint8_t>{}(value.month) ^ std::hash<uint8_t>{}(value.day); |
| 170 | + } |
| 171 | +}; |
| 172 | + |
| 173 | +template <> |
| 174 | +struct hash<frpc::Time> { |
| 175 | + size_t operator()(const frpc::Time& value) const { |
| 176 | + return std::hash<uint8_t>{}(value.hour) ^ |
| 177 | + std::hash<uint8_t>{}(value.minute) ^ |
| 178 | + std::hash<uint8_t>{}(value.second) ^ |
| 179 | + std::hash<uint32_t>{}(value.microsecond); |
| 180 | + } |
| 181 | +}; |
| 182 | + |
| 183 | +template <> |
| 184 | +struct hash<frpc::DateTime> { |
| 185 | + size_t operator()(const frpc::DateTime& value) const { |
| 186 | + return std::hash<frpc::Date>{}(value.date) ^ std::hash<frpc::Time>{}(value.time); |
| 187 | + } |
| 188 | +}; |
| 189 | + |
| 190 | +} // namespace std |
| 191 | + |
| 192 | +#endif // _FRPC_DATE_TIME_H_ |
0 commit comments