|
@@ -0,0 +1,52 @@
|
|
|
|
|
+#include "DateTime.h"
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+namespace DateTime {
|
|
|
|
|
+
|
|
|
|
|
+std::chrono::system_clock::time_point DateTime_UTC_to_time_point(const DateTime& dateTime)
|
|
|
|
|
+{
|
|
|
|
|
+ auto yearMonthDay = date::year(dateTime.year)/dateTime.month/dateTime.day;
|
|
|
|
|
+ if (!yearMonthDay.ok())
|
|
|
|
|
+ throw std::runtime_error("Invalid date");
|
|
|
|
|
+
|
|
|
|
|
+ return date::sys_days(yearMonthDay) + std::chrono::hours(dateTime.hour) + std::chrono::minutes(dateTime.min) + std::chrono::seconds(dateTime.sec);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+std::chrono::system_clock::time_point DateTime_to_time_point(const DateTime& dateTime, const date::time_zone* timeZone)
|
|
|
|
|
+{
|
|
|
|
|
+ auto yearMonthDay = date::year(dateTime.year)/dateTime.month/dateTime.day;
|
|
|
|
|
+ if (!yearMonthDay.ok())
|
|
|
|
|
+ throw std::runtime_error("Invalid date");
|
|
|
|
|
+
|
|
|
|
|
+ return date::make_zoned(timeZone, date::local_days{yearMonthDay} + std::chrono::hours(dateTime.hour) + std::chrono::minutes(dateTime.min) + std::chrono::seconds(dateTime.sec)).get_sys_time();
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+int GetTimestamp()
|
|
|
|
|
+{
|
|
|
|
|
+ return std::time(nullptr);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+int GetUTCOffset()
|
|
|
|
|
+{
|
|
|
|
|
+ std::time_t current_time;
|
|
|
|
|
+ std::time(¤t_time);
|
|
|
|
|
+ struct std::tm *timeinfo = std::localtime(¤t_time);
|
|
|
|
|
+ return timeinfo->tm_gmtoff;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+std::string GetTimeString()
|
|
|
|
|
+{
|
|
|
|
|
+ return GetTimeString(std::chrono::system_clock::now());
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+std::string GetTimeString(const std::chrono::system_clock::time_point& timePoint)
|
|
|
|
|
+{
|
|
|
|
|
+ auto time_t = std::chrono::system_clock::to_time_t(timePoint);
|
|
|
|
|
+ auto tm = *std::localtime(&time_t);
|
|
|
|
|
+
|
|
|
|
|
+ char buffer[80];
|
|
|
|
|
+ strftime(buffer, 80, "%Y-%m-%d %H:%M:%S", &tm);
|
|
|
|
|
+ return std::string(buffer);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+} // namespace DateTime
|