Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-23 09:13:00

0001 // Protocol Buffers - Google's data interchange format
0002 // Copyright 2008 Google Inc.  All rights reserved.
0003 //
0004 // Use of this source code is governed by a BSD-style
0005 // license that can be found in the LICENSE file or at
0006 // https://developers.google.com/open-source/licenses/bsd
0007 
0008 // Defines utilities for the Timestamp and Duration well known types.
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 // Must be included last.
0022 #include "google/protobuf/port_def.inc"
0023 
0024 namespace google {
0025 namespace protobuf {
0026 namespace util {
0027 
0028 // Utility functions for Timestamp and Duration.
0029 class PROTOBUF_EXPORT TimeUtil {
0030   typedef google::protobuf::Timestamp Timestamp;
0031   typedef google::protobuf::Duration Duration;
0032 
0033  public:
0034   // The min/max Timestamp/Duration values we support.
0035   //
0036   // For "0001-01-01T00:00:00Z".
0037   static constexpr int64_t kTimestampMinSeconds = -62135596800LL;
0038   // For "9999-12-31T23:59:59.999999999Z".
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   // Converts Timestamp to/from RFC 3339 date string format.
0064   // Generated output will always be Z-normalized and uses 3, 6 or 9
0065   // fractional digits as required to represent the exact time. When
0066   // parsing, any fractional digits (or none) and any offset are
0067   // accepted as long as they fit into nano-seconds precision.
0068   // Note that Timestamp can only represent time from
0069   // 0001-01-01T00:00:00Z to 9999-12-31T23:59:59.999999999Z. Converting
0070   // a Timestamp outside of this range is undefined behavior.
0071   // See https://www.ietf.org/rfc/rfc3339.txt
0072   //
0073   // Example of generated format:
0074   //   "1972-01-01T10:00:20.021Z"
0075   //
0076   // Example of accepted format:
0077   //   "1972-01-01T10:00:20.021-05:00"
0078   static std::string ToString(const Timestamp& timestamp);
0079   static bool FromString(absl::string_view value, Timestamp* timestamp);
0080 
0081   // Converts Duration to/from string format. The string format will contains
0082   // 3, 6, or 9 fractional digits depending on the precision required to
0083   // represent the exact Duration value. For example:
0084   //   "1s", "1.010s", "1.000000100s", "-3.100s"
0085   // The range that can be represented by Duration is from -315,576,000,000
0086   // to +315,576,000,000 inclusive (in seconds).
0087   static std::string ToString(const Duration& duration);
0088   static bool FromString(absl::string_view value, Duration* duration);
0089 
0090   // Gets the current UTC time.
0091   static Timestamp GetCurrentTime();
0092   // Returns the Time representing "1970-01-01 00:00:00".
0093   static Timestamp GetEpoch();
0094 
0095   // Converts between Duration and integer types. The behavior is undefined if
0096   // the input value is not in the valid range of Duration.
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   // Result will be truncated towards zero. For example, "-1.5s" will be
0104   // truncated to "-1s", and "1.5s" to "1s" when converting to seconds.
0105   // It's undefined behavior if the input duration is not valid or the result
0106   // exceeds the range of int64. A duration is not valid if it's not in the
0107   // valid range of Duration, or have an invalid nanos value (i.e., larger
0108   // than 999999999, less than -999999999, or have a different sign from the
0109   // seconds part).
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   // Creates Timestamp from integer types. The integer value indicates the
0117   // time elapsed from Epoch time. The behavior is undefined if the input
0118   // value is not in the valid range of Timestamp.
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   // Result will be truncated down to the nearest integer value. For example,
0124   // with "1969-12-31T23:59:59.9Z", TimestampToMilliseconds() returns -100
0125   // and TimestampToSeconds() returns -1. It's undefined behavior if the input
0126   // Timestamp is not valid (i.e., its seconds part or nanos part does not fall
0127   // in the valid range) or the return value doesn't fit into int64.
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   // Conversion to/from other time/date types. Note that these types may
0134   // have a different precision and time range from Timestamp/Duration.
0135   // When converting to a lower precision type, the value will be truncated
0136   // to the nearest value that can be represented. If the value is
0137   // out of the range of the result type, the return value is undefined.
0138   //
0139   // Conversion to/from time_t
0140   static Timestamp TimeTToTimestamp(time_t value);
0141   static time_t TimestampToTimeT(const Timestamp& value);
0142 
0143   // Conversion to/from timeval
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 }  // namespace util
0151 }  // namespace protobuf
0152 }  // namespace google
0153 
0154 namespace google {
0155 namespace protobuf {
0156 // Overloaded operators for Duration.
0157 //
0158 // Assignment operators.
0159 PROTOBUF_EXPORT Duration& operator+=(Duration& d1,
0160                                      const Duration& d2);  // NOLINT
0161 PROTOBUF_EXPORT Duration& operator-=(Duration& d1,
0162                                      const Duration& d2);     // NOLINT
0163 PROTOBUF_EXPORT Duration& operator*=(Duration& d, int64_t r);  // NOLINT
0164 PROTOBUF_EXPORT Duration& operator*=(Duration& d, double r);  // NOLINT
0165 PROTOBUF_EXPORT Duration& operator/=(Duration& d, int64_t r);  // NOLINT
0166 PROTOBUF_EXPORT Duration& operator/=(Duration& d, double r);  // NOLINT
0167 // Overload for other integer types.
0168 template <typename T>
0169 Duration& operator*=(Duration& d, T r) {  // NOLINT
0170   int64_t x = r;
0171   return d *= x;
0172 }
0173 template <typename T>
0174 Duration& operator/=(Duration& d, T r) {  // NOLINT
0175   int64_t x = r;
0176   return d /= x;
0177 }
0178 PROTOBUF_EXPORT Duration& operator%=(Duration& d1,
0179                                      const Duration& d2);  // NOLINT
0180 // Relational operators.
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 // Additive operators
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 // Multiplicative operators
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 // Overloaded operators for Timestamp
0243 //
0244 // Assignment operators.
0245 PROTOBUF_EXPORT Timestamp& operator+=(Timestamp& t,
0246                                       const Duration& d);  // NOLINT
0247 PROTOBUF_EXPORT Timestamp& operator-=(Timestamp& t,
0248                                       const Duration& d);  // NOLINT
0249 // Relational operators.
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 // Additive operators.
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 }  // namespace protobuf
0292 }  // namespace google
0293 
0294 #include "google/protobuf/port_undef.inc"
0295 
0296 #endif  // GOOGLE_PROTOBUF_UTIL_TIME_UTIL_H__