诸暨麻将添加redis
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

313 righe
12 KiB

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