Implement more functions for working with local_time

This commit is contained in:
CommanderRedYT
2024-01-12 18:37:48 +01:00
parent fa9eac2afc
commit 729cc1a891
2 changed files with 21 additions and 0 deletions

View File

@@ -215,6 +215,20 @@ utc_clock::time_point fromDateTime(DateTime ts)
};
}
local_clock::time_point fromDateTime(LocalDateTime ts)
{
const sys_days date = ts.date;
return local_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}
, ts.timezone, ts.dst
};
}
std::expected<DateTime, std::string> parseDateTime(std::string_view str)
{
// both valid:
@@ -311,4 +325,9 @@ time_t toTimeT(utc_clock::time_point ts)
{
return std::chrono::duration_cast<std::chrono::seconds>(ts.time_since_epoch()).count();
}
time_t toTimeT(local_clock::time_point ts)
{
return std::chrono::duration_cast<std::chrono::seconds>(ts.time_since_epoch()).count();
}
} // namespace espchrono

View File

@@ -195,6 +195,7 @@ DateTime toDateTime(std::chrono::microseconds ts);
DateTime toDateTime(utc_clock::time_point ts);
LocalDateTime toDateTime(local_clock::time_point ts);
utc_clock::time_point fromDateTime(DateTime ts);
local_clock::time_point fromDateTime(LocalDateTime ts);
std::expected<DateTime, std::string> parseDateTime(std::string_view str);
@@ -214,4 +215,5 @@ std::string toString(minutes32 val);
std::string toString(hours32 val);
time_t toTimeT(utc_clock::time_point ts);
time_t toTimeT(local_clock::time_point ts);
} // namespace espchrono