|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef GOOGLE_PROTOBUF_UTIL_TIME_UTIL_H__
|
|
|
#define GOOGLE_PROTOBUF_UTIL_TIME_UTIL_H__
|
|
|
|
|
|
#include <ctime>
|
|
|
#include <ostream>
|
|
|
#include <string>
|
|
|
#ifdef _MSC_VER
|
|
|
#include <winsock2.h>
|
|
|
#else
|
|
|
#include <sys/time.h>
|
|
|
#endif
|
|
|
|
|
|
#include <google/protobuf/duration.pb.h>
|
|
|
#include <google/protobuf/timestamp.pb.h>
|
|
|
|
|
|
namespace google {
|
|
|
namespace protobuf {
|
|
|
namespace util {
|
|
|
|
|
|
|
|
|
class LIBPROTOBUF_EXPORT TimeUtil {
|
|
|
typedef google::protobuf::Timestamp Timestamp;
|
|
|
typedef google::protobuf::Duration Duration;
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
|
|
static const int64 kTimestampMinSeconds = -62135596800LL;
|
|
|
|
|
|
static const int64 kTimestampMaxSeconds = 253402300799LL;
|
|
|
static const int64 kDurationMinSeconds = -315576000000LL;
|
|
|
static const int64 kDurationMaxSeconds = 315576000000LL;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static string ToString(const Timestamp& timestamp);
|
|
|
static bool FromString(const string& value, Timestamp* timestamp);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static string ToString(const Duration& duration);
|
|
|
static bool FromString(const string& value, Duration* timestamp);
|
|
|
|
|
|
#ifdef GetCurrentTime
|
|
|
#undef GetCurrentTime
|
|
|
#endif
|
|
|
|
|
|
static Timestamp GetCurrentTime();
|
|
|
|
|
|
static Timestamp GetEpoch();
|
|
|
|
|
|
|
|
|
|
|
|
static Duration NanosecondsToDuration(int64 nanos);
|
|
|
static Duration MicrosecondsToDuration(int64 micros);
|
|
|
static Duration MillisecondsToDuration(int64 millis);
|
|
|
static Duration SecondsToDuration(int64 seconds);
|
|
|
static Duration MinutesToDuration(int64 minutes);
|
|
|
static Duration HoursToDuration(int64 hours);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int64 DurationToNanoseconds(const Duration& duration);
|
|
|
static int64 DurationToMicroseconds(const Duration& duration);
|
|
|
static int64 DurationToMilliseconds(const Duration& duration);
|
|
|
static int64 DurationToSeconds(const Duration& duration);
|
|
|
static int64 DurationToMinutes(const Duration& duration);
|
|
|
static int64 DurationToHours(const Duration& duration);
|
|
|
|
|
|
|
|
|
|
|
|
static Timestamp NanosecondsToTimestamp(int64 nanos);
|
|
|
static Timestamp MicrosecondsToTimestamp(int64 micros);
|
|
|
static Timestamp MillisecondsToTimestamp(int64 millis);
|
|
|
static Timestamp SecondsToTimestamp(int64 seconds);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int64 TimestampToNanoseconds(const Timestamp& timestamp);
|
|
|
static int64 TimestampToMicroseconds(const Timestamp& timestamp);
|
|
|
static int64 TimestampToMilliseconds(const Timestamp& timestamp);
|
|
|
static int64 TimestampToSeconds(const Timestamp& timestamp);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static Timestamp TimeTToTimestamp(time_t value);
|
|
|
static time_t TimestampToTimeT(const Timestamp& value);
|
|
|
|
|
|
|
|
|
static Timestamp TimevalToTimestamp(const timeval& value);
|
|
|
static timeval TimestampToTimeval(const Timestamp& value);
|
|
|
static Duration TimevalToDuration(const timeval& value);
|
|
|
static timeval DurationToTimeval(const Duration& value);
|
|
|
};
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
namespace protobuf {
|
|
|
|
|
|
|
|
|
|
|
|
LIBPROTOBUF_EXPORT Duration& operator+=(Duration& d1, const Duration& d2);
|
|
|
LIBPROTOBUF_EXPORT Duration& operator-=(Duration& d1, const Duration& d2);
|
|
|
LIBPROTOBUF_EXPORT Duration& operator*=(Duration& d, int64 r);
|
|
|
LIBPROTOBUF_EXPORT Duration& operator*=(Duration& d, double r);
|
|
|
LIBPROTOBUF_EXPORT Duration& operator/=(Duration& d, int64 r);
|
|
|
LIBPROTOBUF_EXPORT Duration& operator/=(Duration& d, double r);
|
|
|
|
|
|
template <typename T>
|
|
|
Duration& operator*=(Duration& d, T r) {
|
|
|
int64 x = r;
|
|
|
return d *= x;
|
|
|
}
|
|
|
template <typename T>
|
|
|
Duration& operator/=(Duration& d, T r) {
|
|
|
int64 x = r;
|
|
|
return d /= x;
|
|
|
}
|
|
|
LIBPROTOBUF_EXPORT Duration& operator%=(Duration& d1, const Duration& d2);
|
|
|
|
|
|
inline bool operator<(const Duration& d1, const Duration& d2) {
|
|
|
if (d1.seconds() == d2.seconds()) {
|
|
|
return d1.nanos() < d2.nanos();
|
|
|
}
|
|
|
return d1.seconds() < d2.seconds();
|
|
|
}
|
|
|
inline bool operator>(const Duration& d1, const Duration& d2) {
|
|
|
return d2 < d1;
|
|
|
}
|
|
|
inline bool operator>=(const Duration& d1, const Duration& d2) {
|
|
|
return !(d1 < d2);
|
|
|
}
|
|
|
inline bool operator<=(const Duration& d1, const Duration& d2) {
|
|
|
return !(d2 < d1);
|
|
|
}
|
|
|
inline bool operator==(const Duration& d1, const Duration& d2) {
|
|
|
return d1.seconds() == d2.seconds() && d1.nanos() == d2.nanos();
|
|
|
}
|
|
|
inline bool operator!=(const Duration& d1, const Duration& d2) {
|
|
|
return !(d1 == d2);
|
|
|
}
|
|
|
|
|
|
inline Duration operator-(const Duration& d) {
|
|
|
Duration result;
|
|
|
result.set_seconds(-d.seconds());
|
|
|
result.set_nanos(-d.nanos());
|
|
|
return result;
|
|
|
}
|
|
|
inline Duration operator+(const Duration& d1, const Duration& d2) {
|
|
|
Duration result = d1;
|
|
|
return result += d2;
|
|
|
}
|
|
|
inline Duration operator-(const Duration& d1, const Duration& d2) {
|
|
|
Duration result = d1;
|
|
|
return result -= d2;
|
|
|
}
|
|
|
|
|
|
template<typename T>
|
|
|
inline Duration operator*(Duration d, T r) {
|
|
|
return d *= r;
|
|
|
}
|
|
|
template<typename T>
|
|
|
inline Duration operator*(T r, Duration d) {
|
|
|
return d *= r;
|
|
|
}
|
|
|
template<typename T>
|
|
|
inline Duration operator/(Duration d, T r) {
|
|
|
return d /= r;
|
|
|
}
|
|
|
LIBPROTOBUF_EXPORT int64 operator/(const Duration& d1, const Duration& d2);
|
|
|
|
|
|
inline Duration operator%(const Duration& d1, const Duration& d2) {
|
|
|
Duration result = d1;
|
|
|
return result %= d2;
|
|
|
}
|
|
|
|
|
|
inline std::ostream& operator<<(std::ostream& out, const Duration& d) {
|
|
|
out << google::protobuf::util::TimeUtil::ToString(d);
|
|
|
return out;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LIBPROTOBUF_EXPORT Timestamp& operator+=(Timestamp& t, const Duration& d);
|
|
|
LIBPROTOBUF_EXPORT Timestamp& operator-=(Timestamp& t, const Duration& d);
|
|
|
|
|
|
inline bool operator<(const Timestamp& t1, const Timestamp& t2) {
|
|
|
if (t1.seconds() == t2.seconds()) {
|
|
|
return t1.nanos() < t2.nanos();
|
|
|
}
|
|
|
return t1.seconds() < t2.seconds();
|
|
|
}
|
|
|
inline bool operator>(const Timestamp& t1, const Timestamp& t2) {
|
|
|
return t2 < t1;
|
|
|
}
|
|
|
inline bool operator>=(const Timestamp& t1, const Timestamp& t2) {
|
|
|
return !(t1 < t2);
|
|
|
}
|
|
|
inline bool operator<=(const Timestamp& t1, const Timestamp& t2) {
|
|
|
return !(t2 < t1);
|
|
|
}
|
|
|
inline bool operator==(const Timestamp& t1, const Timestamp& t2) {
|
|
|
return t1.seconds() == t2.seconds() && t1.nanos() == t2.nanos();
|
|
|
}
|
|
|
inline bool operator!=(const Timestamp& t1, const Timestamp& t2) {
|
|
|
return !(t1 == t2);
|
|
|
}
|
|
|
|
|
|
inline Timestamp operator+(const Timestamp& t, const Duration& d) {
|
|
|
Timestamp result = t;
|
|
|
return result += d;
|
|
|
}
|
|
|
inline Timestamp operator+(const Duration& d, const Timestamp& t) {
|
|
|
Timestamp result = t;
|
|
|
return result += d;
|
|
|
}
|
|
|
inline Timestamp operator-(const Timestamp& t, const Duration& d) {
|
|
|
Timestamp result = t;
|
|
|
return result -= d;
|
|
|
}
|
|
|
LIBPROTOBUF_EXPORT Duration operator-(const Timestamp& t1, const Timestamp& t2);
|
|
|
|
|
|
inline std::ostream& operator<<(std::ostream& out, const Timestamp& t) {
|
|
|
out << google::protobuf::util::TimeUtil::ToString(t);
|
|
|
return out;
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
}
|
|
|
#endif
|
|
|
|