2019-11-09 19:14:51 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <chrono>
|
|
|
|
#include <string>
|
2020-05-23 22:21:29 +00:00
|
|
|
#include <sstream>
|
2020-07-04 23:16:16 +00:00
|
|
|
#include <cctz/time_zone.h>
|
2019-11-09 19:14:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace ext
|
|
|
|
{
|
2020-03-19 02:17:30 +00:00
|
|
|
inline std::string to_string(const std::time_t & time)
|
|
|
|
{
|
2020-07-04 23:16:16 +00:00
|
|
|
return cctz::format("%Y-%m-%d %H:%M:%S", std::chrono::system_clock::from_time_t(time), cctz::local_time_zone());
|
2020-03-19 02:17:30 +00:00
|
|
|
}
|
|
|
|
|
2019-11-09 19:14:51 +00:00
|
|
|
template <typename Clock, typename Duration = typename Clock::duration>
|
|
|
|
std::string to_string(const std::chrono::time_point<Clock, Duration> & tp)
|
|
|
|
{
|
2020-03-19 02:17:30 +00:00
|
|
|
// Don't use DateLUT because it shows weird characters for
|
|
|
|
// TimePoint::max(). I wish we could use C++20 format, but it's not
|
|
|
|
// there yet.
|
|
|
|
// return DateLUT::instance().timeToString(std::chrono::system_clock::to_time_t(tp));
|
|
|
|
|
|
|
|
auto in_time_t = std::chrono::system_clock::to_time_t(tp);
|
|
|
|
return to_string(in_time_t);
|
2019-11-09 19:14:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Rep, typename Period = std::ratio<1>>
|
2020-08-08 01:21:04 +00:00
|
|
|
std::string to_string(const std::chrono::duration<Rep, Period> & duration)
|
2019-11-09 19:14:51 +00:00
|
|
|
{
|
2020-08-08 01:21:04 +00:00
|
|
|
auto seconds_as_int = std::chrono::duration_cast<std::chrono::seconds>(duration);
|
|
|
|
if (seconds_as_int == duration)
|
2019-11-09 19:14:51 +00:00
|
|
|
return std::to_string(seconds_as_int.count()) + "s";
|
2020-08-08 01:21:04 +00:00
|
|
|
auto seconds_as_double = std::chrono::duration_cast<std::chrono::duration<double>>(duration);
|
2019-11-09 19:14:51 +00:00
|
|
|
return std::to_string(seconds_as_double.count()) + "s";
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Clock, typename Duration = typename Clock::duration>
|
|
|
|
std::ostream & operator<<(std::ostream & o, const std::chrono::time_point<Clock, Duration> & tp)
|
|
|
|
{
|
|
|
|
return o << to_string(tp);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Rep, typename Period = std::ratio<1>>
|
2020-08-08 01:21:04 +00:00
|
|
|
std::ostream & operator<<(std::ostream & o, const std::chrono::duration<Rep, Period> & duration)
|
2019-11-09 19:14:51 +00:00
|
|
|
{
|
2020-08-08 01:21:04 +00:00
|
|
|
return o << to_string(duration);
|
2019-11-09 19:14:51 +00:00
|
|
|
}
|
|
|
|
}
|