blob: b8846935a7887f4877af4a6818163dedc03190f4 [file] [log] [blame]
Brian Silverman9c614bc2016-02-15 20:20:02 -05001// 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
Austin Schuh40c16522018-10-28 20:27:54 -070031// Defines utilities for the Timestamp and Duration well known types.
32
Brian Silverman9c614bc2016-02-15 20:20:02 -050033#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#include <winsock2.h>
41#else
42#include <sys/time.h>
43#endif
44
45#include <google/protobuf/duration.pb.h>
46#include <google/protobuf/timestamp.pb.h>
47
48namespace google {
49namespace protobuf {
50namespace util {
51
Austin Schuh40c16522018-10-28 20:27:54 -070052// Utility functions for Timestamp and Duration.
Brian Silverman9c614bc2016-02-15 20:20:02 -050053class LIBPROTOBUF_EXPORT TimeUtil {
54 typedef google::protobuf::Timestamp Timestamp;
55 typedef google::protobuf::Duration Duration;
56
57 public:
58 // The min/max Timestamp/Duration values we support.
59 //
60 // For "0001-01-01T00:00:00Z".
61 static const int64 kTimestampMinSeconds = -62135596800LL;
62 // For "9999-12-31T23:59:59.999999999Z".
63 static const int64 kTimestampMaxSeconds = 253402300799LL;
64 static const int64 kDurationMinSeconds = -315576000000LL;
65 static const int64 kDurationMaxSeconds = 315576000000LL;
66
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 string ToString(const Timestamp& timestamp);
83 static bool FromString(const string& value, Timestamp* timestamp);
84
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 string ToString(const Duration& duration);
92 static bool FromString(const string& value, Duration* timestamp);
93
94#ifdef GetCurrentTime
95#undef GetCurrentTime // Visual Studio has macro GetCurrentTime
96#endif
97 // Gets the current UTC time.
98 static Timestamp GetCurrentTime();
99 // Returns the Time representing "1970-01-01 00:00:00".
100 static Timestamp GetEpoch();
101
102 // Converts between Duration and integer types. The behavior is undefined if
103 // the input value is not in the valid range of Duration.
104 static Duration NanosecondsToDuration(int64 nanos);
105 static Duration MicrosecondsToDuration(int64 micros);
106 static Duration MillisecondsToDuration(int64 millis);
107 static Duration SecondsToDuration(int64 seconds);
108 static Duration MinutesToDuration(int64 minutes);
109 static Duration HoursToDuration(int64 hours);
110 // Result will be truncated towards zero. For example, "-1.5s" will be
111 // truncated to "-1s", and "1.5s" to "1s" when converting to seconds.
112 // It's undefined behavior if the input duration is not valid or the result
113 // exceeds the range of int64. A duration is not valid if it's not in the
114 // valid range of Duration, or have an invalid nanos value (i.e., larger
115 // than 999999999, less than -999999999, or have a different sign from the
116 // seconds part).
117 static int64 DurationToNanoseconds(const Duration& duration);
118 static int64 DurationToMicroseconds(const Duration& duration);
119 static int64 DurationToMilliseconds(const Duration& duration);
120 static int64 DurationToSeconds(const Duration& duration);
121 static int64 DurationToMinutes(const Duration& duration);
122 static int64 DurationToHours(const Duration& duration);
123 // Creates Timestamp from integer types. The integer value indicates the
124 // time elapsed from Epoch time. The behavior is undefined if the input
125 // value is not in the valid range of Timestamp.
126 static Timestamp NanosecondsToTimestamp(int64 nanos);
127 static Timestamp MicrosecondsToTimestamp(int64 micros);
128 static Timestamp MillisecondsToTimestamp(int64 millis);
129 static Timestamp SecondsToTimestamp(int64 seconds);
130 // Result will be truncated down to the nearest integer value. For example,
131 // with "1969-12-31T23:59:59.9Z", TimestampToMilliseconds() returns -100
132 // and TimestampToSeconds() returns -1. It's undefined behavior if the input
133 // Timestamp is not valid (i.e., its seconds part or nanos part does not fall
134 // in the valid range) or the return value doesn't fit into int64.
135 static int64 TimestampToNanoseconds(const Timestamp& timestamp);
136 static int64 TimestampToMicroseconds(const Timestamp& timestamp);
137 static int64 TimestampToMilliseconds(const Timestamp& timestamp);
138 static int64 TimestampToSeconds(const Timestamp& timestamp);
139
140 // Conversion to/from other time/date types. Note that these types may
141 // have a different precision and time range from Timestamp/Duration.
142 // When converting to a lower precision type, the value will be truncated
143 // to the nearest value that can be represented. If the value is
144 // out of the range of the result type, the return value is undefined.
145 //
146 // Conversion to/from time_t
147 static Timestamp TimeTToTimestamp(time_t value);
148 static time_t TimestampToTimeT(const Timestamp& value);
149
150 // Conversion to/from timeval
151 static Timestamp TimevalToTimestamp(const timeval& value);
152 static timeval TimestampToTimeval(const Timestamp& value);
153 static Duration TimevalToDuration(const timeval& value);
154 static timeval DurationToTimeval(const Duration& value);
155};
156
157} // namespace util
158} // namespace protobuf
159
160
161namespace protobuf {
162// Overloaded operators for Duration.
163//
164// Assignment operators.
165LIBPROTOBUF_EXPORT Duration& operator+=(Duration& d1, const Duration& d2); // NOLINT
166LIBPROTOBUF_EXPORT Duration& operator-=(Duration& d1, const Duration& d2); // NOLINT
167LIBPROTOBUF_EXPORT Duration& operator*=(Duration& d, int64 r); // NOLINT
168LIBPROTOBUF_EXPORT Duration& operator*=(Duration& d, double r); // NOLINT
169LIBPROTOBUF_EXPORT Duration& operator/=(Duration& d, int64 r); // NOLINT
170LIBPROTOBUF_EXPORT Duration& operator/=(Duration& d, double r); // NOLINT
171// Overload for other integer types.
172template <typename T>
173Duration& operator*=(Duration& d, T r) { // NOLINT
174 int64 x = r;
175 return d *= x;
176}
177template <typename T>
178Duration& operator/=(Duration& d, T r) { // NOLINT
179 int64 x = r;
180 return d /= x;
181}
182LIBPROTOBUF_EXPORT Duration& operator%=(Duration& d1, const Duration& d2); // NOLINT
183// Relational operators.
184inline bool operator<(const Duration& d1, const Duration& d2) {
185 if (d1.seconds() == d2.seconds()) {
186 return d1.nanos() < d2.nanos();
187 }
188 return d1.seconds() < d2.seconds();
189}
190inline bool operator>(const Duration& d1, const Duration& d2) {
191 return d2 < d1;
192}
193inline bool operator>=(const Duration& d1, const Duration& d2) {
194 return !(d1 < d2);
195}
196inline bool operator<=(const Duration& d1, const Duration& d2) {
197 return !(d2 < d1);
198}
199inline bool operator==(const Duration& d1, const Duration& d2) {
200 return d1.seconds() == d2.seconds() && d1.nanos() == d2.nanos();
201}
202inline bool operator!=(const Duration& d1, const Duration& d2) {
203 return !(d1 == d2);
204}
205// Additive operators
206inline Duration operator-(const Duration& d) {
207 Duration result;
208 result.set_seconds(-d.seconds());
209 result.set_nanos(-d.nanos());
210 return result;
211}
212inline Duration operator+(const Duration& d1, const Duration& d2) {
213 Duration result = d1;
214 return result += d2;
215}
216inline Duration operator-(const Duration& d1, const Duration& d2) {
217 Duration result = d1;
218 return result -= d2;
219}
220// Multiplicative operators
221template<typename T>
222inline Duration operator*(Duration d, T r) {
223 return d *= r;
224}
225template<typename T>
226inline Duration operator*(T r, Duration d) {
227 return d *= r;
228}
229template<typename T>
230inline Duration operator/(Duration d, T r) {
231 return d /= r;
232}
233LIBPROTOBUF_EXPORT int64 operator/(const Duration& d1, const Duration& d2);
234
235inline Duration operator%(const Duration& d1, const Duration& d2) {
236 Duration result = d1;
237 return result %= d2;
238}
239
Austin Schuh40c16522018-10-28 20:27:54 -0700240inline std::ostream& operator<<(std::ostream& out, const Duration& d) {
Brian Silverman9c614bc2016-02-15 20:20:02 -0500241 out << google::protobuf::util::TimeUtil::ToString(d);
242 return out;
243}
244
245// Overloaded operators for Timestamp
246//
247// Assignement operators.
248LIBPROTOBUF_EXPORT Timestamp& operator+=(Timestamp& t, const Duration& d); // NOLINT
249LIBPROTOBUF_EXPORT Timestamp& operator-=(Timestamp& t, const Duration& d); // NOLINT
250// Relational operators.
251inline bool operator<(const Timestamp& t1, const Timestamp& t2) {
252 if (t1.seconds() == t2.seconds()) {
253 return t1.nanos() < t2.nanos();
254 }
255 return t1.seconds() < t2.seconds();
256}
257inline bool operator>(const Timestamp& t1, const Timestamp& t2) {
258 return t2 < t1;
259}
260inline bool operator>=(const Timestamp& t1, const Timestamp& t2) {
261 return !(t1 < t2);
262}
263inline bool operator<=(const Timestamp& t1, const Timestamp& t2) {
264 return !(t2 < t1);
265}
266inline bool operator==(const Timestamp& t1, const Timestamp& t2) {
267 return t1.seconds() == t2.seconds() && t1.nanos() == t2.nanos();
268}
269inline bool operator!=(const Timestamp& t1, const Timestamp& t2) {
270 return !(t1 == t2);
271}
272// Additive operators.
273inline Timestamp operator+(const Timestamp& t, const Duration& d) {
274 Timestamp result = t;
275 return result += d;
276}
277inline Timestamp operator+(const Duration& d, const Timestamp& t) {
278 Timestamp result = t;
279 return result += d;
280}
281inline Timestamp operator-(const Timestamp& t, const Duration& d) {
282 Timestamp result = t;
283 return result -= d;
284}
285LIBPROTOBUF_EXPORT Duration operator-(const Timestamp& t1, const Timestamp& t2);
286
Austin Schuh40c16522018-10-28 20:27:54 -0700287inline std::ostream& operator<<(std::ostream& out, const Timestamp& t) {
Brian Silverman9c614bc2016-02-15 20:20:02 -0500288 out << google::protobuf::util::TimeUtil::ToString(t);
289 return out;
290}
291
292} // namespace protobuf
293
294
295} // namespace google
296#endif // GOOGLE_PROTOBUF_UTIL_TIME_UTIL_H__