Add fromDateTime()

This commit is contained in:
2023-03-31 13:22:53 +02:00
parent cdbb81e97d
commit eff94f175c
2 changed files with 15 additions and 2 deletions

View File

@ -133,7 +133,6 @@ local_clock::time_point utcToLocal(utc_clock::time_point ts)
} }
#endif #endif
namespace {
DateTime toDateTime(std::chrono::microseconds ts) DateTime toDateTime(std::chrono::microseconds ts)
{ {
auto _time = ts.count(); auto _time = ts.count();
@ -189,7 +188,6 @@ DateTime toDateTime(std::chrono::microseconds ts)
dateTime.dayOfWeek = dayOfWeek; dateTime.dayOfWeek = dayOfWeek;
return dateTime; return dateTime;
} }
}
DateTime toDateTime(utc_clock::time_point ts) DateTime toDateTime(utc_clock::time_point ts)
{ {
@ -204,6 +202,19 @@ LocalDateTime toDateTime(local_clock::time_point ts)
return dateTime; return dateTime;
} }
utc_clock::time_point fromDateTime(DateTime ts)
{
const sys_days date = ts.date;
return utc_clock::time_point {
date.time_since_epoch()
+ std::chrono::hours{ts.hour}
+ std::chrono::minutes{ts.minute}
+ std::chrono::seconds{ts.second}
+ std::chrono::milliseconds{ts.millisecond}
+ std::chrono::microseconds{ts.microsecond}
};
}
std::expected<DateTime, std::string> parseDateTime(std::string_view str) std::expected<DateTime, std::string> parseDateTime(std::string_view str)
{ {
// both valid: // both valid:

View File

@ -191,8 +191,10 @@ utc_clock::time_point localToUtc(local_clock::time_point local);
local_clock::time_point utcToLocal(utc_clock::time_point ts); local_clock::time_point utcToLocal(utc_clock::time_point ts);
#endif #endif
DateTime toDateTime(std::chrono::microseconds ts);
DateTime toDateTime(utc_clock::time_point ts); DateTime toDateTime(utc_clock::time_point ts);
LocalDateTime toDateTime(local_clock::time_point ts); LocalDateTime toDateTime(local_clock::time_point ts);
utc_clock::time_point fromDateTime(DateTime ts);
std::expected<DateTime, std::string> parseDateTime(std::string_view str); std::expected<DateTime, std::string> parseDateTime(std::string_view str);