| OLD | NEW |
| (Empty) | |
| 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 |
| 31 #ifndef GOOGLE_PROTOBUF_UTIL_TIME_UTIL_H__ |
| 32 #define GOOGLE_PROTOBUF_UTIL_TIME_UTIL_H__ |
| 33 |
| 34 #include <ctime> |
| 35 #include <ostream> |
| 36 #include <string> |
| 37 #ifdef _MSC_VER |
| 38 #include <winsock2.h> |
| 39 #else |
| 40 #include <sys/time.h> |
| 41 #endif |
| 42 |
| 43 #include <google/protobuf/duration.pb.h> |
| 44 #include <google/protobuf/timestamp.pb.h> |
| 45 |
| 46 namespace google { |
| 47 namespace protobuf { |
| 48 namespace util { |
| 49 |
| 50 class LIBPROTOBUF_EXPORT TimeUtil { |
| 51 typedef google::protobuf::Timestamp Timestamp; |
| 52 typedef google::protobuf::Duration Duration; |
| 53 |
| 54 public: |
| 55 // The min/max Timestamp/Duration values we support. |
| 56 // |
| 57 // For "0001-01-01T00:00:00Z". |
| 58 static const int64 kTimestampMinSeconds = -62135596800LL; |
| 59 // For "9999-12-31T23:59:59.999999999Z". |
| 60 static const int64 kTimestampMaxSeconds = 253402300799LL; |
| 61 static const int64 kDurationMinSeconds = -315576000000LL; |
| 62 static const int64 kDurationMaxSeconds = 315576000000LL; |
| 63 |
| 64 // Converts Timestamp to/from RFC 3339 date string format. |
| 65 // Generated output will always be Z-normalized and uses 3, 6 or 9 |
| 66 // fractional digits as required to represent the exact time. When |
| 67 // parsing, any fractional digits (or none) and any offset are |
| 68 // accepted as long as they fit into nano-seconds precision. |
| 69 // Note that Timestamp can only represent time from |
| 70 // 0001-01-01T00:00:00Z to 9999-12-31T23:59:59.999999999Z. Converting |
| 71 // a Timestamp outside of this range is undefined behavior. |
| 72 // See https://www.ietf.org/rfc/rfc3339.txt |
| 73 // |
| 74 // Example of generated format: |
| 75 // "1972-01-01T10:00:20.021Z" |
| 76 // |
| 77 // Example of accepted format: |
| 78 // "1972-01-01T10:00:20.021-05:00" |
| 79 static string ToString(const Timestamp& timestamp); |
| 80 static bool FromString(const string& value, Timestamp* timestamp); |
| 81 |
| 82 // Converts Duration to/from string format. The string format will contains |
| 83 // 3, 6, or 9 fractional digits depending on the precision required to |
| 84 // represent the exact Duration value. For example: |
| 85 // "1s", "1.010s", "1.000000100s", "-3.100s" |
| 86 // The range that can be represented by Duration is from -315,576,000,000 |
| 87 // to +315,576,000,000 inclusive (in seconds). |
| 88 static string ToString(const Duration& duration); |
| 89 static bool FromString(const string& value, Duration* timestamp); |
| 90 |
| 91 #ifdef GetCurrentTime |
| 92 #undef GetCurrentTime // Visual Studio has macro GetCurrentTime |
| 93 #endif |
| 94 // Gets the current UTC time. |
| 95 static Timestamp GetCurrentTime(); |
| 96 // Returns the Time representing "1970-01-01 00:00:00". |
| 97 static Timestamp GetEpoch(); |
| 98 |
| 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 |
| 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 |
| 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 |
| 154 } // namespace util |
| 155 } // namespace protobuf |
| 156 |
| 157 |
| 158 namespace protobuf { |
| 159 // Overloaded operators for Duration. |
| 160 // |
| 161 // Assignment operators. |
| 162 LIBPROTOBUF_EXPORT Duration& operator+=(Duration& d1, const Duration& d2); // N
OLINT |
| 163 LIBPROTOBUF_EXPORT Duration& operator-=(Duration& d1, const Duration& d2); // N
OLINT |
| 164 LIBPROTOBUF_EXPORT Duration& operator*=(Duration& d, int64 r); // NOLINT |
| 165 LIBPROTOBUF_EXPORT Duration& operator*=(Duration& d, double r); // NOLINT |
| 166 LIBPROTOBUF_EXPORT Duration& operator/=(Duration& d, int64 r); // NOLINT |
| 167 LIBPROTOBUF_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 x = r; |
| 172 return d *= x; |
| 173 } |
| 174 template <typename T> |
| 175 Duration& operator/=(Duration& d, T r) { // NOLINT |
| 176 int64 x = r; |
| 177 return d /= x; |
| 178 } |
| 179 LIBPROTOBUF_EXPORT Duration& operator%=(Duration& d1, const Duration& d2); // N
OLINT |
| 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 LIBPROTOBUF_EXPORT int64 operator/(const Duration& d1, const Duration& d2); |
| 231 |
| 232 inline Duration operator%(const Duration& d1, const Duration& d2) { |
| 233 Duration result = d1; |
| 234 return result %= d2; |
| 235 } |
| 236 |
| 237 inline ostream& operator<<(ostream& out, const Duration& d) { |
| 238 out << google::protobuf::util::TimeUtil::ToString(d); |
| 239 return out; |
| 240 } |
| 241 |
| 242 // Overloaded operators for Timestamp |
| 243 // |
| 244 // Assignement operators. |
| 245 LIBPROTOBUF_EXPORT Timestamp& operator+=(Timestamp& t, const Duration& d); // N
OLINT |
| 246 LIBPROTOBUF_EXPORT Timestamp& operator-=(Timestamp& t, const Duration& d); // N
OLINT |
| 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 LIBPROTOBUF_EXPORT Duration operator-(const Timestamp& t1, const Timestamp& t2); |
| 283 |
| 284 inline ostream& operator<<(ostream& out, const Timestamp& t) { |
| 285 out << google::protobuf::util::TimeUtil::ToString(t); |
| 286 return out; |
| 287 } |
| 288 |
| 289 } // namespace protobuf |
| 290 |
| 291 |
| 292 } // namespace google |
| 293 #endif // GOOGLE_PROTOBUF_UTIL_TIME_UTIL_H__ |
| OLD | NEW |