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