2022-06-08 23:25:50 +02:00
|
|
|
#ifndef OPENMW_COMPONENTS_MISC_TIMECONVERT_H
|
|
|
|
#define OPENMW_COMPONENTS_MISC_TIMECONVERT_H
|
|
|
|
|
2022-06-11 23:38:09 +02:00
|
|
|
#include <chrono>
|
|
|
|
|
2022-06-08 23:25:50 +02:00
|
|
|
namespace Misc
|
|
|
|
{
|
2022-06-11 23:38:09 +02:00
|
|
|
template <typename TP>
|
2022-09-24 16:20:42 +02:00
|
|
|
inline std::time_t toTimeT(TP tp)
|
2022-06-08 23:25:50 +02:00
|
|
|
{
|
2022-06-11 23:38:09 +02:00
|
|
|
using namespace std::chrono;
|
|
|
|
auto sctp = time_point_cast<system_clock::duration>(tp - TP::clock::now() + system_clock::now());
|
|
|
|
return system_clock::to_time_t(sctp);
|
2022-06-08 23:25:50 +02:00
|
|
|
}
|
2022-09-24 16:20:42 +02:00
|
|
|
|
|
|
|
inline std::string timeTToString(const std::time_t tp, const char* fmt)
|
|
|
|
{
|
|
|
|
tm time_info{};
|
|
|
|
#ifdef _WIN32
|
|
|
|
(void)localtime_s(&time_info, &tp);
|
|
|
|
#else
|
|
|
|
(void)localtime_r(&tp, &time_info);
|
|
|
|
#endif
|
|
|
|
std::stringstream out;
|
|
|
|
out << std::put_time(&time_info, fmt);
|
|
|
|
return out.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline std::string fileTimeToString(const std::filesystem::file_time_type& tp, const char* fmt)
|
|
|
|
{
|
|
|
|
return timeTToString(toTimeT(tp), fmt);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline std::string timeToString(const std::chrono::system_clock::time_point& tp, const char* fmt)
|
|
|
|
{
|
|
|
|
return timeTToString(std::chrono::system_clock::to_time_t(tp), fmt);
|
|
|
|
}
|
2022-06-08 23:25:50 +02:00
|
|
|
} // namespace Misc
|
|
|
|
|
|
|
|
#endif
|