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


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:07:00