#pragma once #include #include #include #ifndef WIN32 #include #endif #include "lib/utils/datetime.h" #include "lib/utils/bts_assert.h" #include "bts_export.h" namespace google { namespace protobuf { class Message; class Timestamp; }} 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 bool from_string(const std::string &str, T*out) { return google::protobuf::TextFormat::ParseFromString (str,out); } template 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 }}}