Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-28 10:10:21

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 #ifdef _MSC_VER
0018 #ifdef _XBOX_ONE
0019 struct timeval {
0020   int64_t tv_sec;  /* seconds */
0021   int64_t tv_usec; /* and microseconds */
0022 };
0023 #else
0024 #include <winsock2.h>
0025 #endif  // _XBOX_ONE
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 // Must be included last.
0034 #include "google/protobuf/port_def.inc"
0035 
0036 namespace google {
0037 namespace protobuf {
0038 namespace util {
0039 
0040 // Utility functions for Timestamp and Duration.
0041 class PROTOBUF_EXPORT TimeUtil {
0042   typedef google::protobuf::Timestamp Timestamp;
0043   typedef google::protobuf::Duration Duration;
0044 
0045  public:
0046   // The min/max Timestamp/Duration values we support.
0047   //
0048   // For "0001-01-01T00:00:00Z".
0049   static constexpr int64_t kTimestampMinSeconds = -62135596800LL;
0050   // For "9999-12-31T23:59:59.999999999Z".
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   // Converts Timestamp to/from RFC 3339 date string format.
0076   // Generated output will always be Z-normalized and uses 3, 6 or 9
0077   // fractional digits as required to represent the exact time. When
0078   // parsing, any fractional digits (or none) and any offset are
0079   // accepted as long as they fit into nano-seconds precision.
0080   // Note that Timestamp can only represent time from
0081   // 0001-01-01T00:00:00Z to 9999-12-31T23:59:59.999999999Z. Converting
0082   // a Timestamp outside of this range is undefined behavior.
0083   // See https://www.ietf.org/rfc/rfc3339.txt
0084   //
0085   // Example of generated format:
0086   //   "1972-01-01T10:00:20.021Z"
0087   //
0088   // Example of accepted format:
0089   //   "1972-01-01T10:00:20.021-05:00"
0090   static std::string ToString(const Timestamp& timestamp);
0091   static bool FromString(absl::string_view value, Timestamp* timestamp);
0092 
0093   // Converts Duration to/from string format. The string format will contains
0094   // 3, 6, or 9 fractional digits depending on the precision required to
0095   // represent the exact Duration value. For example:
0096   //   "1s", "1.010s", "1.000000100s", "-3.100s"
0097   // The range that can be represented by Duration is from -315,576,000,000
0098   // to +315,576,000,000 inclusive (in seconds).
0099   static std::string ToString(const Duration& duration);
0100   static bool FromString(absl::string_view value, Duration* duration);
0101 
0102   // Gets the current UTC time.
0103   static Timestamp GetCurrentTime();
0104   // Returns the Time representing "1970-01-01 00:00:00".
0105   static Timestamp GetEpoch();
0106 
0107   // Converts between Duration and integer types. The behavior is undefined if
0108   // the input value is not in the valid range of Duration.
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   // Result will be truncated towards zero. For example, "-1.5s" will be
0116   // truncated to "-1s", and "1.5s" to "1s" when converting to seconds.
0117   // It's undefined behavior if the input duration is not valid or the result
0118   // exceeds the range of int64. A duration is not valid if it's not in the
0119   // valid range of Duration, or have an invalid nanos value (i.e., larger
0120   // than 999999999, less than -999999999, or have a different sign from the
0121   // seconds part).
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   // Creates Timestamp from integer types. The integer value indicates the
0129   // time elapsed from Epoch time. The behavior is undefined if the input
0130   // value is not in the valid range of Timestamp.
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   // Result will be truncated down to the nearest integer value. For example,
0136   // with "1969-12-31T23:59:59.9Z", TimestampToMilliseconds() returns -100
0137   // and TimestampToSeconds() returns -1. It's undefined behavior if the input
0138   // Timestamp is not valid (i.e., its seconds part or nanos part does not fall
0139   // in the valid range) or the return value doesn't fit into int64.
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   // Conversion to/from other time/date types. Note that these types may
0146   // have a different precision and time range from Timestamp/Duration.
0147   // When converting to a lower precision type, the value will be truncated
0148   // to the nearest value that can be represented. If the value is
0149   // out of the range of the result type, the return value is undefined.
0150   //
0151   // Conversion to/from time_t
0152   static Timestamp TimeTToTimestamp(time_t value);
0153   static time_t TimestampToTimeT(const Timestamp& value);
0154 
0155   // Conversion to/from timeval
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 }  // namespace util
0163 }  // namespace protobuf
0164 }  // namespace google
0165 
0166 namespace google {
0167 namespace protobuf {
0168 // Overloaded operators for Duration.
0169 //
0170 // Assignment operators.
0171 PROTOBUF_EXPORT Duration& operator+=(Duration& d1,
0172                                      const Duration& d2);  // NOLINT
0173 PROTOBUF_EXPORT Duration& operator-=(Duration& d1,
0174                                      const Duration& d2);     // NOLINT
0175 PROTOBUF_EXPORT Duration& operator*=(Duration& d, int64_t r);  // NOLINT
0176 PROTOBUF_EXPORT Duration& operator*=(Duration& d, double r);  // NOLINT
0177 PROTOBUF_EXPORT Duration& operator/=(Duration& d, int64_t r);  // NOLINT
0178 PROTOBUF_EXPORT Duration& operator/=(Duration& d, double r);  // NOLINT
0179 // Overload for other integer types.
0180 template <typename T>
0181 Duration& operator*=(Duration& d, T r) {  // NOLINT
0182   int64_t x = r;
0183   return d *= x;
0184 }
0185 template <typename T>
0186 Duration& operator/=(Duration& d, T r) {  // NOLINT
0187   int64_t x = r;
0188   return d /= x;
0189 }
0190 PROTOBUF_EXPORT Duration& operator%=(Duration& d1,
0191                                      const Duration& d2);  // NOLINT
0192 // Relational operators.
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 // Additive operators
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 // Multiplicative operators
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 // Overloaded operators for Timestamp
0255 //
0256 // Assignment operators.
0257 PROTOBUF_EXPORT Timestamp& operator+=(Timestamp& t,
0258                                       const Duration& d);  // NOLINT
0259 PROTOBUF_EXPORT Timestamp& operator-=(Timestamp& t,
0260                                       const Duration& d);  // NOLINT
0261 // Relational operators.
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 // Additive operators.
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 }  // namespace protobuf
0304 }  // namespace google
0305 
0306 #include "google/protobuf/port_undef.inc"
0307 
0308 #endif  // GOOGLE_PROTOBUF_UTIL_TIME_UTIL_H__