File indexing completed on 2025-02-28 10:10:21
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #ifndef GOOGLE_PROTOBUF_UTIL_TIME_UTIL_H__
0011 #define GOOGLE_PROTOBUF_UTIL_TIME_UTIL_H__
0012
0013 #include <cstdint>
0014 #include <ctime>
0015 #include <ostream>
0016 #include <string>
0017 #ifdef _MSC_VER
0018 #ifdef _XBOX_ONE
0019 struct timeval {
0020 int64_t tv_sec;
0021 int64_t tv_usec;
0022 };
0023 #else
0024 #include <winsock2.h>
0025 #endif
0026 #else
0027 #include <sys/time.h>
0028 #endif
0029
0030 #include "google/protobuf/duration.pb.h"
0031 #include "google/protobuf/timestamp.pb.h"
0032
0033
0034 #include "google/protobuf/port_def.inc"
0035
0036 namespace google {
0037 namespace protobuf {
0038 namespace util {
0039
0040
0041 class PROTOBUF_EXPORT TimeUtil {
0042 typedef google::protobuf::Timestamp Timestamp;
0043 typedef google::protobuf::Duration Duration;
0044
0045 public:
0046
0047
0048
0049 static constexpr int64_t kTimestampMinSeconds = -62135596800LL;
0050
0051 static constexpr int64_t kTimestampMaxSeconds = 253402300799LL;
0052 static constexpr int32_t kTimestampMinNanoseconds = 0;
0053 static constexpr int32_t kTimestampMaxNanoseconds = 999999999;
0054 static constexpr int64_t kDurationMinSeconds = -315576000000LL;
0055 static constexpr int64_t kDurationMaxSeconds = 315576000000LL;
0056 static constexpr int32_t kDurationMinNanoseconds = -999999999;
0057 static constexpr int32_t kDurationMaxNanoseconds = 999999999;
0058
0059 static bool IsTimestampValid(const Timestamp& timestamp) {
0060 return timestamp.seconds() <= kTimestampMaxSeconds &&
0061 timestamp.seconds() >= kTimestampMinSeconds &&
0062 timestamp.nanos() <= kTimestampMaxNanoseconds &&
0063 timestamp.nanos() >= kTimestampMinNanoseconds;
0064 }
0065
0066 static bool IsDurationValid(const Duration& duration) {
0067 return duration.seconds() <= kDurationMaxSeconds &&
0068 duration.seconds() >= kDurationMinSeconds &&
0069 duration.nanos() <= kDurationMaxNanoseconds &&
0070 duration.nanos() >= kDurationMinNanoseconds &&
0071 !(duration.seconds() >= 1 && duration.nanos() < 0) &&
0072 !(duration.seconds() <= -1 && duration.nanos() > 0);
0073 }
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090 static std::string ToString(const Timestamp& timestamp);
0091 static bool FromString(absl::string_view value, Timestamp* timestamp);
0092
0093
0094
0095
0096
0097
0098
0099 static std::string ToString(const Duration& duration);
0100 static bool FromString(absl::string_view value, Duration* duration);
0101
0102
0103 static Timestamp GetCurrentTime();
0104
0105 static Timestamp GetEpoch();
0106
0107
0108
0109 static Duration NanosecondsToDuration(int64_t nanos);
0110 static Duration MicrosecondsToDuration(int64_t micros);
0111 static Duration MillisecondsToDuration(int64_t millis);
0112 static Duration SecondsToDuration(int64_t seconds);
0113 static Duration MinutesToDuration(int64_t minutes);
0114 static Duration HoursToDuration(int64_t hours);
0115
0116
0117
0118
0119
0120
0121
0122 static int64_t DurationToNanoseconds(const Duration& duration);
0123 static int64_t DurationToMicroseconds(const Duration& duration);
0124 static int64_t DurationToMilliseconds(const Duration& duration);
0125 static int64_t DurationToSeconds(const Duration& duration);
0126 static int64_t DurationToMinutes(const Duration& duration);
0127 static int64_t DurationToHours(const Duration& duration);
0128
0129
0130
0131 static Timestamp NanosecondsToTimestamp(int64_t nanos);
0132 static Timestamp MicrosecondsToTimestamp(int64_t micros);
0133 static Timestamp MillisecondsToTimestamp(int64_t millis);
0134 static Timestamp SecondsToTimestamp(int64_t seconds);
0135
0136
0137
0138
0139
0140 static int64_t TimestampToNanoseconds(const Timestamp& timestamp);
0141 static int64_t TimestampToMicroseconds(const Timestamp& timestamp);
0142 static int64_t TimestampToMilliseconds(const Timestamp& timestamp);
0143 static int64_t TimestampToSeconds(const Timestamp& timestamp);
0144
0145
0146
0147
0148
0149
0150
0151
0152 static Timestamp TimeTToTimestamp(time_t value);
0153 static time_t TimestampToTimeT(const Timestamp& value);
0154
0155
0156 static Timestamp TimevalToTimestamp(const timeval& value);
0157 static timeval TimestampToTimeval(const Timestamp& value);
0158 static Duration TimevalToDuration(const timeval& value);
0159 static timeval DurationToTimeval(const Duration& value);
0160 };
0161
0162 }
0163 }
0164 }
0165
0166 namespace google {
0167 namespace protobuf {
0168
0169
0170
0171 PROTOBUF_EXPORT Duration& operator+=(Duration& d1,
0172 const Duration& d2);
0173 PROTOBUF_EXPORT Duration& operator-=(Duration& d1,
0174 const Duration& d2);
0175 PROTOBUF_EXPORT Duration& operator*=(Duration& d, int64_t r);
0176 PROTOBUF_EXPORT Duration& operator*=(Duration& d, double r);
0177 PROTOBUF_EXPORT Duration& operator/=(Duration& d, int64_t r);
0178 PROTOBUF_EXPORT Duration& operator/=(Duration& d, double r);
0179
0180 template <typename T>
0181 Duration& operator*=(Duration& d, T r) {
0182 int64_t x = r;
0183 return d *= x;
0184 }
0185 template <typename T>
0186 Duration& operator/=(Duration& d, T r) {
0187 int64_t x = r;
0188 return d /= x;
0189 }
0190 PROTOBUF_EXPORT Duration& operator%=(Duration& d1,
0191 const Duration& d2);
0192
0193 inline bool operator<(const Duration& d1, const Duration& d2) {
0194 if (d1.seconds() == d2.seconds()) {
0195 return d1.nanos() < d2.nanos();
0196 }
0197 return d1.seconds() < d2.seconds();
0198 }
0199 inline bool operator>(const Duration& d1, const Duration& d2) {
0200 return d2 < d1;
0201 }
0202 inline bool operator>=(const Duration& d1, const Duration& d2) {
0203 return !(d1 < d2);
0204 }
0205 inline bool operator<=(const Duration& d1, const Duration& d2) {
0206 return !(d2 < d1);
0207 }
0208 inline bool operator==(const Duration& d1, const Duration& d2) {
0209 return d1.seconds() == d2.seconds() && d1.nanos() == d2.nanos();
0210 }
0211 inline bool operator!=(const Duration& d1, const Duration& d2) {
0212 return !(d1 == d2);
0213 }
0214
0215 inline Duration operator-(const Duration& d) {
0216 Duration result;
0217 result.set_seconds(-d.seconds());
0218 result.set_nanos(-d.nanos());
0219 return result;
0220 }
0221 inline Duration operator+(const Duration& d1, const Duration& d2) {
0222 Duration result = d1;
0223 return result += d2;
0224 }
0225 inline Duration operator-(const Duration& d1, const Duration& d2) {
0226 Duration result = d1;
0227 return result -= d2;
0228 }
0229
0230 template <typename T>
0231 inline Duration operator*(Duration d, T r) {
0232 return d *= r;
0233 }
0234 template <typename T>
0235 inline Duration operator*(T r, Duration d) {
0236 return d *= r;
0237 }
0238 template <typename T>
0239 inline Duration operator/(Duration d, T r) {
0240 return d /= r;
0241 }
0242 PROTOBUF_EXPORT int64_t operator/(const Duration& d1, const Duration& d2);
0243
0244 inline Duration operator%(const Duration& d1, const Duration& d2) {
0245 Duration result = d1;
0246 return result %= d2;
0247 }
0248
0249 inline std::ostream& operator<<(std::ostream& out, const Duration& d) {
0250 out << google::protobuf::util::TimeUtil::ToString(d);
0251 return out;
0252 }
0253
0254
0255
0256
0257 PROTOBUF_EXPORT Timestamp& operator+=(Timestamp& t,
0258 const Duration& d);
0259 PROTOBUF_EXPORT Timestamp& operator-=(Timestamp& t,
0260 const Duration& d);
0261
0262 inline bool operator<(const Timestamp& t1, const Timestamp& t2) {
0263 if (t1.seconds() == t2.seconds()) {
0264 return t1.nanos() < t2.nanos();
0265 }
0266 return t1.seconds() < t2.seconds();
0267 }
0268 inline bool operator>(const Timestamp& t1, const Timestamp& t2) {
0269 return t2 < t1;
0270 }
0271 inline bool operator>=(const Timestamp& t1, const Timestamp& t2) {
0272 return !(t1 < t2);
0273 }
0274 inline bool operator<=(const Timestamp& t1, const Timestamp& t2) {
0275 return !(t2 < t1);
0276 }
0277 inline bool operator==(const Timestamp& t1, const Timestamp& t2) {
0278 return t1.seconds() == t2.seconds() && t1.nanos() == t2.nanos();
0279 }
0280 inline bool operator!=(const Timestamp& t1, const Timestamp& t2) {
0281 return !(t1 == t2);
0282 }
0283
0284 inline Timestamp operator+(const Timestamp& t, const Duration& d) {
0285 Timestamp result = t;
0286 return result += d;
0287 }
0288 inline Timestamp operator+(const Duration& d, const Timestamp& t) {
0289 Timestamp result = t;
0290 return result += d;
0291 }
0292 inline Timestamp operator-(const Timestamp& t, const Duration& d) {
0293 Timestamp result = t;
0294 return result -= d;
0295 }
0296 PROTOBUF_EXPORT Duration operator-(const Timestamp& t1, const Timestamp& t2);
0297
0298 inline std::ostream& operator<<(std::ostream& out, const Timestamp& t) {
0299 out << google::protobuf::util::TimeUtil::ToString(t);
0300 return out;
0301 }
0302
0303 }
0304 }
0305
0306 #include "google/protobuf/port_undef.inc"
0307
0308 #endif