time_util.h 11 KB

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