time_util.h 12 KB

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