You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

time_util.h 12 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. // Defines utilities for the Timestamp and Duration well known types.
  31. #ifndef GOOGLE_PROTOBUF_UTIL_TIME_UTIL_H__
  32. #define GOOGLE_PROTOBUF_UTIL_TIME_UTIL_H__
  33. #include <cstdint>
  34. #include <ctime>
  35. #include <ostream>
  36. #include <string>
  37. #ifdef _MSC_VER
  38. #ifdef _XBOX_ONE
  39. struct timeval {
  40. int64_t tv_sec; /* seconds */
  41. int64_t tv_usec; /* and microseconds */
  42. };
  43. #else
  44. #include <winsock2.h>
  45. #endif // _XBOX_ONE
  46. #else
  47. #include <sys/time.h>
  48. #endif
  49. #include <google/protobuf/duration.pb.h>
  50. #include <google/protobuf/timestamp.pb.h>
  51. // Must be included last.
  52. #include <google/protobuf/port_def.inc>
  53. namespace google {
  54. namespace protobuf {
  55. namespace util {
  56. // Utility functions for Timestamp and Duration.
  57. class PROTOBUF_EXPORT TimeUtil {
  58. typedef google::protobuf::Timestamp Timestamp;
  59. typedef google::protobuf::Duration Duration;
  60. public:
  61. // The min/max Timestamp/Duration values we support.
  62. //
  63. // For "0001-01-01T00:00:00Z".
  64. static const int64_t kTimestampMinSeconds = -62135596800LL;
  65. // For "9999-12-31T23:59:59.999999999Z".
  66. static const int64_t kTimestampMaxSeconds = 253402300799LL;
  67. static const int64_t kDurationMinSeconds = -315576000000LL;
  68. static const int64_t kDurationMaxSeconds = 315576000000LL;
  69. // Converts Timestamp to/from RFC 3339 date string format.
  70. // Generated output will always be Z-normalized and uses 3, 6 or 9
  71. // fractional digits as required to represent the exact time. When
  72. // parsing, any fractional digits (or none) and any offset are
  73. // accepted as long as they fit into nano-seconds precision.
  74. // Note that Timestamp can only represent time from
  75. // 0001-01-01T00:00:00Z to 9999-12-31T23:59:59.999999999Z. Converting
  76. // a Timestamp outside of this range is undefined behavior.
  77. // See https://www.ietf.org/rfc/rfc3339.txt
  78. //
  79. // Example of generated format:
  80. // "1972-01-01T10:00:20.021Z"
  81. //
  82. // Example of accepted format:
  83. // "1972-01-01T10:00:20.021-05:00"
  84. static std::string ToString(const Timestamp& timestamp);
  85. static bool FromString(const std::string& value, Timestamp* timestamp);
  86. // Converts Duration to/from string format. The string format will contains
  87. // 3, 6, or 9 fractional digits depending on the precision required to
  88. // represent the exact Duration value. For example:
  89. // "1s", "1.010s", "1.000000100s", "-3.100s"
  90. // The range that can be represented by Duration is from -315,576,000,000
  91. // to +315,576,000,000 inclusive (in seconds).
  92. static std::string ToString(const Duration& duration);
  93. static bool FromString(const std::string& value, Duration* timestamp);
  94. #ifdef GetCurrentTime
  95. #undef GetCurrentTime // Visual Studio has macro GetCurrentTime
  96. #endif
  97. // Gets the current UTC time.
  98. static Timestamp GetCurrentTime();
  99. // Returns the Time representing "1970-01-01 00:00:00".
  100. static Timestamp GetEpoch();
  101. // Converts between Duration and integer types. The behavior is undefined if
  102. // the input value is not in the valid range of Duration.
  103. static Duration NanosecondsToDuration(int64_t nanos);
  104. static Duration MicrosecondsToDuration(int64_t micros);
  105. static Duration MillisecondsToDuration(int64_t millis);
  106. static Duration SecondsToDuration(int64_t seconds);
  107. static Duration MinutesToDuration(int64_t minutes);
  108. static Duration HoursToDuration(int64_t hours);
  109. // Result will be truncated towards zero. For example, "-1.5s" will be
  110. // truncated to "-1s", and "1.5s" to "1s" when converting to seconds.
  111. // It's undefined behavior if the input duration is not valid or the result
  112. // exceeds the range of int64. A duration is not valid if it's not in the
  113. // valid range of Duration, or have an invalid nanos value (i.e., larger
  114. // than 999999999, less than -999999999, or have a different sign from the
  115. // seconds part).
  116. static int64_t DurationToNanoseconds(const Duration& duration);
  117. static int64_t DurationToMicroseconds(const Duration& duration);
  118. static int64_t DurationToMilliseconds(const Duration& duration);
  119. static int64_t DurationToSeconds(const Duration& duration);
  120. static int64_t DurationToMinutes(const Duration& duration);
  121. static int64_t DurationToHours(const Duration& duration);
  122. // Creates Timestamp from integer types. The integer value indicates the
  123. // time elapsed from Epoch time. The behavior is undefined if the input
  124. // value is not in the valid range of Timestamp.
  125. static Timestamp NanosecondsToTimestamp(int64_t nanos);
  126. static Timestamp MicrosecondsToTimestamp(int64_t micros);
  127. static Timestamp MillisecondsToTimestamp(int64_t millis);
  128. static Timestamp SecondsToTimestamp(int64_t seconds);
  129. // Result will be truncated down to the nearest integer value. For example,
  130. // with "1969-12-31T23:59:59.9Z", TimestampToMilliseconds() returns -100
  131. // and TimestampToSeconds() returns -1. It's undefined behavior if the input
  132. // Timestamp is not valid (i.e., its seconds part or nanos part does not fall
  133. // in the valid range) or the return value doesn't fit into int64.
  134. static int64_t TimestampToNanoseconds(const Timestamp& timestamp);
  135. static int64_t TimestampToMicroseconds(const Timestamp& timestamp);
  136. static int64_t TimestampToMilliseconds(const Timestamp& timestamp);
  137. static int64_t TimestampToSeconds(const Timestamp& timestamp);
  138. // Conversion to/from other time/date types. Note that these types may
  139. // have a different precision and time range from Timestamp/Duration.
  140. // When converting to a lower precision type, the value will be truncated
  141. // to the nearest value that can be represented. If the value is
  142. // out of the range of the result type, the return value is undefined.
  143. //
  144. // Conversion to/from time_t
  145. static Timestamp TimeTToTimestamp(time_t value);
  146. static time_t TimestampToTimeT(const Timestamp& value);
  147. // Conversion to/from timeval
  148. static Timestamp TimevalToTimestamp(const timeval& value);
  149. static timeval TimestampToTimeval(const Timestamp& value);
  150. static Duration TimevalToDuration(const timeval& value);
  151. static timeval DurationToTimeval(const Duration& value);
  152. };
  153. } // namespace util
  154. } // namespace protobuf
  155. } // namespace google
  156. namespace google {
  157. namespace protobuf {
  158. // Overloaded operators for Duration.
  159. //
  160. // Assignment operators.
  161. PROTOBUF_EXPORT Duration& operator+=(Duration& d1,
  162. const Duration& d2); // NOLINT
  163. PROTOBUF_EXPORT Duration& operator-=(Duration& d1,
  164. const Duration& d2); // NOLINT
  165. PROTOBUF_EXPORT Duration& operator*=(Duration& d, int64_t r); // NOLINT
  166. PROTOBUF_EXPORT Duration& operator*=(Duration& d, double r); // NOLINT
  167. PROTOBUF_EXPORT Duration& operator/=(Duration& d, int64_t r); // NOLINT
  168. PROTOBUF_EXPORT Duration& operator/=(Duration& d, double r); // NOLINT
  169. // Overload for other integer types.
  170. template <typename T>
  171. Duration& operator*=(Duration& d, T r) { // NOLINT
  172. int64_t x = r;
  173. return d *= x;
  174. }
  175. template <typename T>
  176. Duration& operator/=(Duration& d, T r) { // NOLINT
  177. int64_t x = r;
  178. return d /= x;
  179. }
  180. PROTOBUF_EXPORT Duration& operator%=(Duration& d1,
  181. const Duration& d2); // NOLINT
  182. // Relational operators.
  183. inline bool operator<(const Duration& d1, const Duration& d2) {
  184. if (d1.seconds() == d2.seconds()) {
  185. return d1.nanos() < d2.nanos();
  186. }
  187. return d1.seconds() < d2.seconds();
  188. }
  189. inline bool operator>(const Duration& d1, const Duration& d2) {
  190. return d2 < d1;
  191. }
  192. inline bool operator>=(const Duration& d1, const Duration& d2) {
  193. return !(d1 < d2);
  194. }
  195. inline bool operator<=(const Duration& d1, const Duration& d2) {
  196. return !(d2 < d1);
  197. }
  198. inline bool operator==(const Duration& d1, const Duration& d2) {
  199. return d1.seconds() == d2.seconds() && d1.nanos() == d2.nanos();
  200. }
  201. inline bool operator!=(const Duration& d1, const Duration& d2) {
  202. return !(d1 == d2);
  203. }
  204. // Additive operators
  205. inline Duration operator-(const Duration& d) {
  206. Duration result;
  207. result.set_seconds(-d.seconds());
  208. result.set_nanos(-d.nanos());
  209. return result;
  210. }
  211. inline Duration operator+(const Duration& d1, const Duration& d2) {
  212. Duration result = d1;
  213. return result += d2;
  214. }
  215. inline Duration operator-(const Duration& d1, const Duration& d2) {
  216. Duration result = d1;
  217. return result -= d2;
  218. }
  219. // Multiplicative operators
  220. template <typename T>
  221. inline Duration operator*(Duration d, T r) {
  222. return d *= r;
  223. }
  224. template <typename T>
  225. inline Duration operator*(T r, Duration d) {
  226. return d *= r;
  227. }
  228. template <typename T>
  229. inline Duration operator/(Duration d, T r) {
  230. return d /= r;
  231. }
  232. PROTOBUF_EXPORT int64_t operator/(const Duration& d1, const Duration& d2);
  233. inline Duration operator%(const Duration& d1, const Duration& d2) {
  234. Duration result = d1;
  235. return result %= d2;
  236. }
  237. inline std::ostream& operator<<(std::ostream& out, const Duration& d) {
  238. out << ::PROTOBUF_NAMESPACE_ID::util::TimeUtil::ToString(d);
  239. return out;
  240. }
  241. // Overloaded operators for Timestamp
  242. //
  243. // Assignment operators.
  244. PROTOBUF_EXPORT Timestamp& operator+=(Timestamp& t,
  245. const Duration& d); // NOLINT
  246. PROTOBUF_EXPORT Timestamp& operator-=(Timestamp& t,
  247. const Duration& d); // NOLINT
  248. // Relational operators.
  249. inline bool operator<(const Timestamp& t1, const Timestamp& t2) {
  250. if (t1.seconds() == t2.seconds()) {
  251. return t1.nanos() < t2.nanos();
  252. }
  253. return t1.seconds() < t2.seconds();
  254. }
  255. inline bool operator>(const Timestamp& t1, const Timestamp& t2) {
  256. return t2 < t1;
  257. }
  258. inline bool operator>=(const Timestamp& t1, const Timestamp& t2) {
  259. return !(t1 < t2);
  260. }
  261. inline bool operator<=(const Timestamp& t1, const Timestamp& t2) {
  262. return !(t2 < t1);
  263. }
  264. inline bool operator==(const Timestamp& t1, const Timestamp& t2) {
  265. return t1.seconds() == t2.seconds() && t1.nanos() == t2.nanos();
  266. }
  267. inline bool operator!=(const Timestamp& t1, const Timestamp& t2) {
  268. return !(t1 == t2);
  269. }
  270. // Additive operators.
  271. inline Timestamp operator+(const Timestamp& t, const Duration& d) {
  272. Timestamp result = t;
  273. return result += d;
  274. }
  275. inline Timestamp operator+(const Duration& d, const Timestamp& t) {
  276. Timestamp result = t;
  277. return result += d;
  278. }
  279. inline Timestamp operator-(const Timestamp& t, const Duration& d) {
  280. Timestamp result = t;
  281. return result -= d;
  282. }
  283. PROTOBUF_EXPORT Duration operator-(const Timestamp& t1, const Timestamp& t2);
  284. inline std::ostream& operator<<(std::ostream& out, const Timestamp& t) {
  285. out << ::PROTOBUF_NAMESPACE_ID::util::TimeUtil::ToString(t);
  286. return out;
  287. }
  288. } // namespace protobuf
  289. } // namespace google
  290. #include <google/protobuf/port_undef.inc>
  291. #endif // GOOGLE_PROTOBUF_UTIL_TIME_UTIL_H__