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 15 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. {
  41. int64_t tv_sec; /* seconds */
  42. int64_t tv_usec; /* and microseconds */
  43. };
  44. #else
  45. #include <winsock2.h>
  46. #endif // _XBOX_ONE
  47. #else
  48. #include <sys/time.h>
  49. #endif
  50. #include <google/protobuf/duration.pb.h>
  51. #include <google/protobuf/timestamp.pb.h>
  52. // Must be included last.
  53. #include <google/protobuf/port_def.inc>
  54. namespace google
  55. {
  56. namespace protobuf
  57. {
  58. namespace util
  59. {
  60. // Utility functions for Timestamp and Duration.
  61. class PROTOBUF_EXPORT TimeUtil
  62. {
  63. typedef google::protobuf::Timestamp Timestamp;
  64. typedef google::protobuf::Duration Duration;
  65. public:
  66. // The min/max Timestamp/Duration values we support.
  67. //
  68. // For "0001-01-01T00:00:00Z".
  69. static const int64_t kTimestampMinSeconds = -62135596800LL;
  70. // For "9999-12-31T23:59:59.999999999Z".
  71. static const int64_t kTimestampMaxSeconds = 253402300799LL;
  72. static const int64_t kDurationMinSeconds = -315576000000LL;
  73. static const int64_t kDurationMaxSeconds = 315576000000LL;
  74. // Converts Timestamp to/from RFC 3339 date string format.
  75. // Generated output will always be Z-normalized and uses 3, 6 or 9
  76. // fractional digits as required to represent the exact time. When
  77. // parsing, any fractional digits (or none) and any offset are
  78. // accepted as long as they fit into nano-seconds precision.
  79. // Note that Timestamp can only represent time from
  80. // 0001-01-01T00:00:00Z to 9999-12-31T23:59:59.999999999Z. Converting
  81. // a Timestamp outside of this range is undefined behavior.
  82. // See https://www.ietf.org/rfc/rfc3339.txt
  83. //
  84. // Example of generated format:
  85. // "1972-01-01T10:00:20.021Z"
  86. //
  87. // Example of accepted format:
  88. // "1972-01-01T10:00:20.021-05:00"
  89. static std::string ToString(const Timestamp& timestamp);
  90. static bool FromString(const std::string& value, Timestamp* timestamp);
  91. // Converts Duration to/from string format. The string format will contains
  92. // 3, 6, or 9 fractional digits depending on the precision required to
  93. // represent the exact Duration value. For example:
  94. // "1s", "1.010s", "1.000000100s", "-3.100s"
  95. // The range that can be represented by Duration is from -315,576,000,000
  96. // to +315,576,000,000 inclusive (in seconds).
  97. static std::string ToString(const Duration& duration);
  98. static bool FromString(const std::string& value, Duration* timestamp);
  99. #ifdef GetCurrentTime
  100. #undef GetCurrentTime // Visual Studio has macro GetCurrentTime
  101. #endif
  102. // Gets the current UTC time.
  103. static Timestamp GetCurrentTime();
  104. // Returns the Time representing "1970-01-01 00:00:00".
  105. static Timestamp GetEpoch();
  106. // Converts between Duration and integer types. The behavior is undefined if
  107. // the input value is not in the valid range of Duration.
  108. static Duration NanosecondsToDuration(int64_t nanos);
  109. static Duration MicrosecondsToDuration(int64_t micros);
  110. static Duration MillisecondsToDuration(int64_t millis);
  111. static Duration SecondsToDuration(int64_t seconds);
  112. static Duration MinutesToDuration(int64_t minutes);
  113. static Duration HoursToDuration(int64_t hours);
  114. // Result will be truncated towards zero. For example, "-1.5s" will be
  115. // truncated to "-1s", and "1.5s" to "1s" when converting to seconds.
  116. // It's undefined behavior if the input duration is not valid or the result
  117. // exceeds the range of int64. A duration is not valid if it's not in the
  118. // valid range of Duration, or have an invalid nanos value (i.e., larger
  119. // than 999999999, less than -999999999, or have a different sign from the
  120. // seconds part).
  121. static int64_t DurationToNanoseconds(const Duration& duration);
  122. static int64_t DurationToMicroseconds(const Duration& duration);
  123. static int64_t DurationToMilliseconds(const Duration& duration);
  124. static int64_t DurationToSeconds(const Duration& duration);
  125. static int64_t DurationToMinutes(const Duration& duration);
  126. static int64_t DurationToHours(const Duration& duration);
  127. // Creates Timestamp from integer types. The integer value indicates the
  128. // time elapsed from Epoch time. The behavior is undefined if the input
  129. // value is not in the valid range of Timestamp.
  130. static Timestamp NanosecondsToTimestamp(int64_t nanos);
  131. static Timestamp MicrosecondsToTimestamp(int64_t micros);
  132. static Timestamp MillisecondsToTimestamp(int64_t millis);
  133. static Timestamp SecondsToTimestamp(int64_t seconds);
  134. // Result will be truncated down to the nearest integer value. For example,
  135. // with "1969-12-31T23:59:59.9Z", TimestampToMilliseconds() returns -100
  136. // and TimestampToSeconds() returns -1. It's undefined behavior if the input
  137. // Timestamp is not valid (i.e., its seconds part or nanos part does not fall
  138. // in the valid range) or the return value doesn't fit into int64.
  139. static int64_t TimestampToNanoseconds(const Timestamp& timestamp);
  140. static int64_t TimestampToMicroseconds(const Timestamp& timestamp);
  141. static int64_t TimestampToMilliseconds(const Timestamp& timestamp);
  142. static int64_t TimestampToSeconds(const Timestamp& timestamp);
  143. // Conversion to/from other time/date types. Note that these types may
  144. // have a different precision and time range from Timestamp/Duration.
  145. // When converting to a lower precision type, the value will be truncated
  146. // to the nearest value that can be represented. If the value is
  147. // out of the range of the result type, the return value is undefined.
  148. //
  149. // Conversion to/from time_t
  150. static Timestamp TimeTToTimestamp(time_t value);
  151. static time_t TimestampToTimeT(const Timestamp& value);
  152. // Conversion to/from timeval
  153. static Timestamp TimevalToTimestamp(const timeval& value);
  154. static timeval TimestampToTimeval(const Timestamp& value);
  155. static Duration TimevalToDuration(const timeval& value);
  156. static timeval DurationToTimeval(const Duration& value);
  157. };
  158. } // namespace util
  159. } // namespace protobuf
  160. } // namespace google
  161. namespace google
  162. {
  163. namespace protobuf
  164. {
  165. // Overloaded operators for Duration.
  166. //
  167. // Assignment operators.
  168. PROTOBUF_EXPORT Duration& operator+=(Duration& d1,
  169. const Duration& d2); // NOLINT
  170. PROTOBUF_EXPORT Duration& operator-=(Duration& d1,
  171. const Duration& d2); // NOLINT
  172. PROTOBUF_EXPORT Duration& operator*=(Duration& d, int64_t r); // NOLINT
  173. PROTOBUF_EXPORT Duration& operator*=(Duration& d, double r); // NOLINT
  174. PROTOBUF_EXPORT Duration& operator/=(Duration& d, int64_t r); // NOLINT
  175. PROTOBUF_EXPORT Duration& operator/=(Duration& d, double r); // NOLINT
  176. // Overload for other integer types.
  177. template<typename T>
  178. Duration& operator*=(Duration& d, T r)
  179. { // NOLINT
  180. int64_t x = r;
  181. return d *= x;
  182. }
  183. template<typename T>
  184. Duration& operator/=(Duration& d, T r)
  185. { // NOLINT
  186. int64_t x = r;
  187. return d /= x;
  188. }
  189. PROTOBUF_EXPORT Duration& operator%=(Duration& d1,
  190. const Duration& d2); // NOLINT
  191. // Relational operators.
  192. inline bool operator<(const Duration& d1, const Duration& d2)
  193. {
  194. if (d1.seconds() == d2.seconds())
  195. {
  196. return d1.nanos() < d2.nanos();
  197. }
  198. return d1.seconds() < d2.seconds();
  199. }
  200. inline bool operator>(const Duration& d1, const Duration& d2)
  201. {
  202. return d2 < d1;
  203. }
  204. inline bool operator>=(const Duration& d1, const Duration& d2)
  205. {
  206. return !(d1 < d2);
  207. }
  208. inline bool operator<=(const Duration& d1, const Duration& d2)
  209. {
  210. return !(d2 < d1);
  211. }
  212. inline bool operator==(const Duration& d1, const Duration& d2)
  213. {
  214. return d1.seconds() == d2.seconds() && d1.nanos() == d2.nanos();
  215. }
  216. inline bool operator!=(const Duration& d1, const Duration& d2)
  217. {
  218. return !(d1 == d2);
  219. }
  220. // Additive operators
  221. inline Duration operator-(const Duration& d)
  222. {
  223. Duration result;
  224. result.set_seconds(-d.seconds());
  225. result.set_nanos(-d.nanos());
  226. return result;
  227. }
  228. inline Duration operator+(const Duration& d1, const Duration& d2)
  229. {
  230. Duration result = d1;
  231. return result += d2;
  232. }
  233. inline Duration operator-(const Duration& d1, const Duration& d2)
  234. {
  235. Duration result = d1;
  236. return result -= d2;
  237. }
  238. // Multiplicative operators
  239. template<typename T>
  240. inline Duration operator*(Duration d, T r)
  241. {
  242. return d *= r;
  243. }
  244. template<typename T>
  245. inline Duration operator*(T r, Duration d)
  246. {
  247. return d *= r;
  248. }
  249. template<typename T>
  250. inline Duration operator/(Duration d, T r)
  251. {
  252. return d /= r;
  253. }
  254. PROTOBUF_EXPORT int64_t operator/(const Duration& d1, const Duration& d2);
  255. inline Duration operator%(const Duration& d1, const Duration& d2)
  256. {
  257. Duration result = d1;
  258. return result %= d2;
  259. }
  260. inline std::ostream& operator<<(std::ostream& out, const Duration& d)
  261. {
  262. out << ::PROTOBUF_NAMESPACE_ID::util::TimeUtil::ToString(d);
  263. return out;
  264. }
  265. // Overloaded operators for Timestamp
  266. //
  267. // Assignment operators.
  268. PROTOBUF_EXPORT Timestamp& operator+=(Timestamp& t,
  269. const Duration& d); // NOLINT
  270. PROTOBUF_EXPORT Timestamp& operator-=(Timestamp& t,
  271. const Duration& d); // NOLINT
  272. // Relational operators.
  273. inline bool operator<(const Timestamp& t1, const Timestamp& t2)
  274. {
  275. if (t1.seconds() == t2.seconds())
  276. {
  277. return t1.nanos() < t2.nanos();
  278. }
  279. return t1.seconds() < t2.seconds();
  280. }
  281. inline bool operator>(const Timestamp& t1, const Timestamp& t2)
  282. {
  283. return t2 < t1;
  284. }
  285. inline bool operator>=(const Timestamp& t1, const Timestamp& t2)
  286. {
  287. return !(t1 < t2);
  288. }
  289. inline bool operator<=(const Timestamp& t1, const Timestamp& t2)
  290. {
  291. return !(t2 < t1);
  292. }
  293. inline bool operator==(const Timestamp& t1, const Timestamp& t2)
  294. {
  295. return t1.seconds() == t2.seconds() && t1.nanos() == t2.nanos();
  296. }
  297. inline bool operator!=(const Timestamp& t1, const Timestamp& t2)
  298. {
  299. return !(t1 == t2);
  300. }
  301. // Additive operators.
  302. inline Timestamp operator+(const Timestamp& t, const Duration& d)
  303. {
  304. Timestamp result = t;
  305. return result += d;
  306. }
  307. inline Timestamp operator+(const Duration& d, const Timestamp& t)
  308. {
  309. Timestamp result = t;
  310. return result += d;
  311. }
  312. inline Timestamp operator-(const Timestamp& t, const Duration& d)
  313. {
  314. Timestamp result = t;
  315. return result -= d;
  316. }
  317. PROTOBUF_EXPORT Duration operator-(const Timestamp& t1, const Timestamp& t2);
  318. inline std::ostream& operator<<(std::ostream& out, const Timestamp& t)
  319. {
  320. out << ::PROTOBUF_NAMESPACE_ID::util::TimeUtil::ToString(t);
  321. return out;
  322. }
  323. } // namespace protobuf
  324. } // namespace google
  325. #include <google/protobuf/port_undef.inc>
  326. #endif // GOOGLE_PROTOBUF_UTIL_TIME_UTIL_H__