17#ifndef CYBER_COMMON_TIME_CONVERSION_H_
18#define CYBER_COMMON_TIME_CONVERSION_H_
47static const std::vector<std::pair<int32_t, int32_t>> LEAP_SECONDS = {
66 for (
size_t i = 0; i < LEAP_SECONDS.size(); ++i) {
67 if (unix_seconds >= LEAP_SECONDS[i].first) {
68 return unix_seconds - (
UNIX_GPS_DIFF - LEAP_SECONDS[i].second);
71 return static_cast<T
>(0);
86 for (
size_t i = 0; i < LEAP_SECONDS.size(); ++i) {
87 T result = gps_seconds + (
UNIX_GPS_DIFF - LEAP_SECONDS[i].second);
88 if ((int32_t)result >= LEAP_SECONDS[i].first) {
92 return static_cast<T
>(0);
116 const std::string& time_str,
117 const std::string& format_str =
"%Y-%m-%d %H:%M:%S") {
119 strptime(time_str.c_str(), format_str.c_str(), &tmp_time);
120 tmp_time.tm_isdst = -1;
121 time_t time = mktime(&tmp_time);
122 return static_cast<uint64_t
>(time);
126 uint64_t unix_seconds,
127 const std::string& format_str =
"%Y-%m-%d-%H:%M:%S") {
128 std::time_t t = unix_seconds;
130 struct tm* ret = localtime_r(&t, &ptm);
131 if (ret ==
nullptr) {
132 return std::string(
"");
134 uint32_t length = 64;
135 std::vector<char> buff(length,
'\0');
136 strftime(buff.data(), length, format_str.c_str(), ret);
137 return std::string(buff.data());
constexpr int32_t UNIX_GPS_DIFF
int64_t GpsToUnixNanoseconds(int64_t gps_nanoseconds)
constexpr int64_t ONE_MILLION
int64_t UnixToGpsNanoseconds(int64_t unix_nanoseconds)
int64_t GpsToUnixMicroseconds(int64_t gps_microseconds)
constexpr int64_t ONE_BILLION
T UnixToGpsSeconds(T unix_seconds)
uint64_t StringToUnixSeconds(const std::string &time_str, const std::string &format_str="%Y-%m-%d %H:%M:%S")
std::string UnixSecondsToString(uint64_t unix_seconds, const std::string &format_str="%Y-%m-%d-%H:%M:%S")
int64_t UnixToGpsMicroseconds(int64_t unix_microseconds)
T GpsToUnixSeconds(T gps_seconds)