87 lines
2.0 KiB
C++
87 lines
2.0 KiB
C++
#pragma once
|
|
|
|
#include <google/protobuf/text_format.h>
|
|
#include <google/protobuf/timestamp.pb.h>
|
|
#include <string>
|
|
|
|
#ifndef WIN32
|
|
#include <sys/time.h>
|
|
#endif
|
|
|
|
#include "bts_export.h"
|
|
#include "lib/utils/bts_assert.h"
|
|
#include "lib/utils/datetime.h"
|
|
|
|
namespace google {
|
|
namespace protobuf {
|
|
class Message;
|
|
class Timestamp;
|
|
} // namespace protobuf
|
|
} // namespace google
|
|
|
|
namespace bts {
|
|
namespace utils {
|
|
namespace pb {
|
|
|
|
BTS_EXPORT std::string to_string(google::protobuf::Message const &msg);
|
|
BTS_EXPORT google::protobuf::Timestamp
|
|
to_timestamp(datetime::PTime const &ptime);
|
|
|
|
template <class T> bool from_string(const std::string &str, T *out) {
|
|
return google::protobuf::TextFormat::ParseFromString(str, out);
|
|
}
|
|
|
|
template <class T> T from_string(const std::string &str) {
|
|
T result;
|
|
bts_assert(google::protobuf::TextFormat::ParseFromString(str, &result));
|
|
return result;
|
|
}
|
|
|
|
#ifndef WIN32
|
|
inline google::protobuf::Timestamp pb_now() {
|
|
::google::protobuf::Timestamp result;
|
|
struct timeval tv;
|
|
gettimeofday(&tv, NULL);
|
|
result.set_seconds(tv.tv_sec);
|
|
result.set_nanos(tv.tv_usec * 1000);
|
|
return result;
|
|
}
|
|
|
|
inline bool smaller(
|
|
google::protobuf::Timestamp const &t1,
|
|
google::protobuf::Timestamp const &t2) {
|
|
if (t1.seconds() < t2.seconds())
|
|
return true;
|
|
else if (t1.seconds() > t2.seconds())
|
|
return false;
|
|
else
|
|
return t1.nanos() < t2.nanos();
|
|
}
|
|
|
|
inline bool greater(
|
|
google::protobuf::Timestamp const &t1,
|
|
google::protobuf::Timestamp const &t2) {
|
|
if (t1.seconds() > t2.seconds())
|
|
return true;
|
|
else if (t1.seconds() < t2.seconds())
|
|
return false;
|
|
else
|
|
return t1.nanos() > t2.nanos();
|
|
}
|
|
|
|
inline bool equal(
|
|
google::protobuf::Timestamp const &t1,
|
|
google::protobuf::Timestamp const &t2) {
|
|
return t1.seconds() == t2.seconds() && t1.nanos() == t2.nanos();
|
|
}
|
|
|
|
inline double to_double_time(google::protobuf::Timestamp const &t1) {
|
|
return t1.seconds() + t1.nanos() / 1e9;
|
|
}
|
|
|
|
#endif
|
|
|
|
} // namespace pb
|
|
} // namespace utils
|
|
} // namespace bts
|