time_util_test.cc
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 
32 
33 #include <ctime>
34 
38 #include <gtest/gtest.h>
39 
40 namespace google {
41 namespace protobuf {
42 namespace util {
43 
46 
47 namespace {
48 
49 TEST(TimeUtilTest, TimestampStringFormat) {
51  EXPECT_TRUE(TimeUtil::FromString("0001-01-01T00:00:00Z", &begin));
53  EXPECT_EQ(0, begin.nanos());
54  EXPECT_TRUE(TimeUtil::FromString("9999-12-31T23:59:59.999999999Z", &end));
56  EXPECT_EQ(999999999, end.nanos());
57  EXPECT_EQ("0001-01-01T00:00:00Z", TimeUtil::ToString(begin));
58  EXPECT_EQ("9999-12-31T23:59:59.999999999Z", TimeUtil::ToString(end));
59 
60  // Test negative timestamps.
62  EXPECT_EQ(-1, time.seconds());
63  // Timestamp's nano part is always non-negative.
64  EXPECT_EQ(999999999, time.nanos());
65  EXPECT_EQ("1969-12-31T23:59:59.999999999Z", TimeUtil::ToString(time));
66 
67  // Generated output should contain 3, 6, or 9 fractional digits.
68  EXPECT_EQ("1970-01-01T00:00:00Z",
70  EXPECT_EQ("1970-01-01T00:00:00.010Z",
72  EXPECT_EQ("1970-01-01T00:00:00.000010Z",
74  EXPECT_EQ("1970-01-01T00:00:00.000000010Z",
76 
77  // Parsing accepts an fractional digits as long as they fit into nano
78  // precision.
79  EXPECT_TRUE(TimeUtil::FromString("1970-01-01T00:00:00.1Z", &time));
81  EXPECT_TRUE(TimeUtil::FromString("1970-01-01T00:00:00.0001Z", &time));
83  EXPECT_TRUE(TimeUtil::FromString("1970-01-01T00:00:00.0000001Z", &time));
85 
86  // Also accpets offsets.
87  EXPECT_TRUE(TimeUtil::FromString("1970-01-01T00:00:00-08:00", &time));
88  EXPECT_EQ(8 * 3600, TimeUtil::TimestampToSeconds(time));
89 }
90 
91 TEST(TimeUtilTest, DurationStringFormat) {
93  EXPECT_TRUE(TimeUtil::FromString("0001-01-01T00:00:00Z", &begin));
94  EXPECT_TRUE(TimeUtil::FromString("9999-12-31T23:59:59.999999999Z", &end));
95 
96  EXPECT_EQ("315537897599.999999999s", TimeUtil::ToString(end - begin));
97  EXPECT_EQ(999999999, (end - begin).nanos());
98  EXPECT_EQ("-315537897599.999999999s", TimeUtil::ToString(begin - end));
99  EXPECT_EQ(-999999999, (begin - end).nanos());
100 
101  // Generated output should contain 3, 6, or 9 fractional digits.
104  EXPECT_EQ("0.000010s",
106  EXPECT_EQ("0.000000010s",
108 
109  // Parsing accepts an fractional digits as long as they fit into nano
110  // precision.
111  Duration d;
112  EXPECT_TRUE(TimeUtil::FromString("0.1s", &d));
114  EXPECT_TRUE(TimeUtil::FromString("0.0001s", &d));
116  EXPECT_TRUE(TimeUtil::FromString("0.0000001s", &d));
118 
119  // Duration must support range from -315,576,000,000s to +315576000000s
120  // which includes negative values.
121  EXPECT_TRUE(TimeUtil::FromString("315576000000.999999999s", &d));
122  EXPECT_EQ(315576000000LL, d.seconds());
123  EXPECT_EQ(999999999, d.nanos());
124  EXPECT_TRUE(TimeUtil::FromString("-315576000000.999999999s", &d));
125  EXPECT_EQ(-315576000000LL, d.seconds());
126  EXPECT_EQ(-999999999, d.nanos());
127 }
128 
129 TEST(TimeUtilTest, GetEpoch) {
131 }
132 
133 TEST(TimeUtilTest, DurationIntegerConversion) {
134  EXPECT_EQ("0.000000001s",
136  EXPECT_EQ("-0.000000001s",
138  EXPECT_EQ("0.000001s",
140  EXPECT_EQ("-0.000001s",
143  EXPECT_EQ("-0.001s",
151 
152  EXPECT_EQ(
154  EXPECT_EQ(
156  EXPECT_EQ(
160  EXPECT_EQ(
170 
171  // Test truncation behavior.
174  // For negative values, Duration will be rounded towards 0.
177 }
178 
179 TEST(TestUtilTest, TimestampIntegerConversion) {
180  EXPECT_EQ("1970-01-01T00:00:00.000000001Z",
182  EXPECT_EQ("1969-12-31T23:59:59.999999999Z",
184  EXPECT_EQ("1970-01-01T00:00:00.000001Z",
186  EXPECT_EQ("1969-12-31T23:59:59.999999Z",
188  EXPECT_EQ("1970-01-01T00:00:00.001Z",
190  EXPECT_EQ("1969-12-31T23:59:59.999Z",
192  EXPECT_EQ("1970-01-01T00:00:01Z",
194  EXPECT_EQ("1969-12-31T23:59:59Z",
196 
197  EXPECT_EQ(
211 
212  // Test truncation behavior.
215  // For negative values, Timestamp will be rounded down.
216  // For example, "1969-12-31T23:59:59.5Z" (i.e., -0.5s) rounded to seconds
217  // will be "1969-12-31T23:59:59Z" (i.e., -1s) rather than
218  // "1970-01-01T00:00:00Z" (i.e., 0s).
221 }
222 
223 TEST(TimeUtilTest, TimeTConversion) {
224  time_t value = time(NULL);
227  EXPECT_EQ(
229 }
230 
231 TEST(TimeUtilTest, TimevalConversion) {
234  EXPECT_EQ(1, value.tv_sec);
235  EXPECT_EQ(999999, value.tv_usec);
237  TimeUtil::NanosecondsToTimestamp(-1999999999));
238  EXPECT_EQ(-2, value.tv_sec);
239  EXPECT_EQ(0, value.tv_usec);
240 
241  value =
243  EXPECT_EQ(1, value.tv_sec);
244  EXPECT_EQ(999999, value.tv_usec);
245  value =
247  EXPECT_EQ(-2, value.tv_sec);
248  EXPECT_EQ(1, value.tv_usec);
249 }
250 
251 TEST(TimeUtilTest, DurationOperators) {
252  Duration one_second = TimeUtil::SecondsToDuration(1);
254 
255  // Test +/-
256  Duration a = one_second;
257  a += one_second;
258  a -= one_nano;
259  EXPECT_EQ("1.999999999s", TimeUtil::ToString(a));
260  Duration b = -a;
261  EXPECT_EQ("-1.999999999s", TimeUtil::ToString(b));
262  EXPECT_EQ("3.999999998s", TimeUtil::ToString(a + a));
263  EXPECT_EQ("0s", TimeUtil::ToString(a + b));
264  EXPECT_EQ("0s", TimeUtil::ToString(b + a));
265  EXPECT_EQ("-3.999999998s", TimeUtil::ToString(b + b));
266  EXPECT_EQ("3.999999998s", TimeUtil::ToString(a - b));
267  EXPECT_EQ("0s", TimeUtil::ToString(a - a));
268  EXPECT_EQ("0s", TimeUtil::ToString(b - b));
269  EXPECT_EQ("-3.999999998s", TimeUtil::ToString(b - a));
270 
271  // Test *
272  EXPECT_EQ(a + a, a * 2);
273  EXPECT_EQ(b + b, a * (-2));
274  EXPECT_EQ(b + b, b * 2);
275  EXPECT_EQ(a + a, b * (-2));
276  EXPECT_EQ("0.999999999s", TimeUtil::ToString(a * 0.5));
277  EXPECT_EQ("-0.999999999s", TimeUtil::ToString(b * 0.5));
278  // Multiplication should not overflow if the result fits into the supported
279  // range of Duration (intermediate result may be larger than int64).
280  EXPECT_EQ("315575999684.424s",
281  TimeUtil::ToString((one_second - one_nano) * 315576000000LL));
282  EXPECT_EQ("-315575999684.424s",
283  TimeUtil::ToString((one_nano - one_second) * 315576000000LL));
284  EXPECT_EQ("-315575999684.424s",
285  TimeUtil::ToString((one_second - one_nano) * (-315576000000LL)));
286 
287  // Test / and %
288  EXPECT_EQ("0.999999999s", TimeUtil::ToString(a / 2));
289  EXPECT_EQ("-0.999999999s", TimeUtil::ToString(b / 2));
290  Duration large = TimeUtil::SecondsToDuration(315576000000LL) - one_nano;
291  // We have to handle division with values beyond 64 bits.
292  EXPECT_EQ("0.999999999s", TimeUtil::ToString(large / 315576000000LL));
293  EXPECT_EQ("-0.999999999s", TimeUtil::ToString((-large) / 315576000000LL));
294  EXPECT_EQ("-0.999999999s", TimeUtil::ToString(large / (-315576000000LL)));
295  Duration large2 = large + one_nano;
296  EXPECT_EQ(large, large % large2);
297  EXPECT_EQ(-large, (-large) % large2);
298  EXPECT_EQ(large, large % (-large2));
299  EXPECT_EQ(one_nano, large2 % large);
300  EXPECT_EQ(-one_nano, (-large2) % large);
301  EXPECT_EQ(one_nano, large2 % (-large));
302  // Some corner cases about negative values.
303  //
304  // (-5) / 2 = -2, remainder = -1
305  // (-5) / (-2) = 2, remainder = -1
310  EXPECT_EQ(-2, a / b);
312  EXPECT_EQ(2, a / (-b));
314 
315  // Test relational operators.
316  EXPECT_TRUE(one_nano < one_second);
317  EXPECT_FALSE(one_second < one_second);
318  EXPECT_FALSE(one_second < one_nano);
319  EXPECT_FALSE(-one_nano < -one_second);
320  EXPECT_FALSE(-one_second < -one_second);
321  EXPECT_TRUE(-one_second < -one_nano);
322  EXPECT_TRUE(-one_nano < one_nano);
323  EXPECT_FALSE(one_nano < -one_nano);
324 
325  EXPECT_FALSE(one_nano > one_second);
326  EXPECT_FALSE(one_nano > one_nano);
327  EXPECT_TRUE(one_second > one_nano);
328 
329  EXPECT_FALSE(one_nano >= one_second);
330  EXPECT_TRUE(one_nano >= one_nano);
331  EXPECT_TRUE(one_second >= one_nano);
332 
333  EXPECT_TRUE(one_nano <= one_second);
334  EXPECT_TRUE(one_nano <= one_nano);
335  EXPECT_FALSE(one_second <= one_nano);
336 
337  EXPECT_TRUE(one_nano == one_nano);
338  EXPECT_FALSE(one_nano == one_second);
339 
340  EXPECT_FALSE(one_nano != one_nano);
341  EXPECT_TRUE(one_nano != one_second);
342 }
343 
344 TEST(TimeUtilTest, TimestampOperators) {
346  EXPECT_TRUE(TimeUtil::FromString("0001-01-01T00:00:00Z", &begin));
347  EXPECT_TRUE(TimeUtil::FromString("9999-12-31T23:59:59.999999999Z", &end));
348  Duration d = end - begin;
349  EXPECT_TRUE(end == begin + d);
350  EXPECT_TRUE(end == d + begin);
351  EXPECT_TRUE(begin == end - d);
352 
353  // Test relational operators
354  Timestamp t1 = begin + d / 4;
355  Timestamp t2 = end - d / 4;
356  EXPECT_TRUE(t1 < t2);
357  EXPECT_FALSE(t1 < t1);
358  EXPECT_FALSE(t2 < t1);
359  EXPECT_FALSE(t1 > t2);
360  EXPECT_FALSE(t1 > t1);
361  EXPECT_TRUE(t2 > t1);
362  EXPECT_FALSE(t1 >= t2);
363  EXPECT_TRUE(t1 >= t1);
364  EXPECT_TRUE(t2 >= t1);
365  EXPECT_TRUE(t1 <= t2);
366  EXPECT_TRUE(t1 <= t1);
367  EXPECT_FALSE(t2 <= t1);
368 
369  EXPECT_FALSE(t1 == t2);
370  EXPECT_TRUE(t1 == t1);
371  EXPECT_FALSE(t2 == t1);
372  EXPECT_TRUE(t1 != t2);
373  EXPECT_FALSE(t1 != t1);
374  EXPECT_TRUE(t2 != t1);
375 }
376 
377 } // namespace
378 } // namespace util
379 } // namespace protobuf
380 } // namespace google
google::protobuf::util::TimeUtil::MicrosecondsToDuration
static Duration MicrosecondsToDuration(int64 micros)
Definition: time_util.cc:246
end
GLuint GLuint end
Definition: glcorearb.h:2858
Timestamp
Definition: timestamp.pb.h:69
NULL
NULL
Definition: test_security_zap.cpp:405
google::protobuf::util::TEST
TEST(DelimitedMessageUtilTest, DelimitedMessages)
Definition: delimited_message_util_test.cc:47
google::protobuf::util::TimeUtil::GetEpoch
static Timestamp GetEpoch()
Definition: time_util.cc:177
gtest.h
EXPECT_EQ
#define EXPECT_EQ(val1, val2)
Definition: glog/src/googletest.h:155
google::protobuf::util::TimeUtil::DurationToMilliseconds
static int64 DurationToMilliseconds(const Duration &duration)
Definition: time_util.cc:279
google::protobuf::util::TimeUtil::TimestampToMilliseconds
static int64 TimestampToMilliseconds(const Timestamp &timestamp)
Definition: time_util.cc:326
duration.pb.h
google::protobuf::util::TimeUtil::DurationToMinutes
static int64 DurationToMinutes(const Duration &duration)
Definition: time_util.cc:288
google::protobuf::util::TimeUtil::DurationToTimeval
static timeval DurationToTimeval(const Duration &value)
Definition: time_util.cc:360
google::protobuf::util::TimeUtil::kTimestampMinSeconds
static const int64 kTimestampMinSeconds
Definition: time_util.h:70
b
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:3228
google::protobuf::util::TimeUtil::DurationToSeconds
static int64 DurationToSeconds(const Duration &duration)
Definition: time_util.cc:284
Timestamp
struct Timestamp Timestamp
Definition: php/ext/google/protobuf/protobuf.h:663
begin
static size_t begin(const upb_table *t)
Definition: php/ext/google/protobuf/upb.c:4898
google::protobuf::util::TimeUtil::TimestampToSeconds
static int64 TimestampToSeconds(const Timestamp &timestamp)
Definition: time_util.cc:331
google::protobuf::util::TimeUtil::TimestampToTimeT
static time_t TimestampToTimeT(const Timestamp &value)
Definition: time_util.cc:339
google::protobuf::util::TimeUtil::FromString
static bool FromString(const std::string &value, Timestamp *timestamp)
Definition: time_util.cc:160
google::protobuf::util::TimeUtil::MinutesToDuration
static Duration MinutesToDuration(int64 minutes)
Definition: time_util.cc:262
google::protobuf::util::TimeUtil::kTimestampMaxSeconds
static const int64 kTimestampMaxSeconds
Definition: time_util.h:72
google::protobuf::util::TimeUtil::DurationToNanoseconds
static int64 DurationToNanoseconds(const Duration &duration)
Definition: time_util.cc:270
Timestamp::nanos
::PROTOBUF_NAMESPACE_ID::int32 nanos() const
Definition: timestamp.pb.h:257
google::protobuf::util::TimeUtil::TimestampToTimeval
static timeval TimestampToTimeval(const Timestamp &value)
Definition: time_util.cc:348
EXPECT_TRUE
#define EXPECT_TRUE(cond)
Definition: glog/src/googletest.h:137
google::protobuf::util::TimeUtil::HoursToDuration
static Duration HoursToDuration(int64 hours)
Definition: time_util.cc:266
d
d
google::protobuf::util::TimeUtil::NanosecondsToTimestamp
static Timestamp NanosecondsToTimestamp(int64 nanos)
Definition: time_util.cc:296
google::protobuf::util::TimeUtil::TimestampToMicroseconds
static int64 TimestampToMicroseconds(const Timestamp &timestamp)
Definition: time_util.cc:321
google::protobuf::util::TimeUtil::MillisecondsToDuration
static Duration MillisecondsToDuration(int64 millis)
Definition: time_util.cc:252
Timestamp::seconds
::PROTOBUF_NAMESPACE_ID::int64 seconds() const
Definition: timestamp.pb.h:243
google::protobuf::util::TimeUtil::DurationToMicroseconds
static int64 DurationToMicroseconds(const Duration &duration)
Definition: time_util.cc:274
google::protobuf::util::TimeUtil::NanosecondsToDuration
static Duration NanosecondsToDuration(int64 nanos)
Definition: time_util.cc:241
LL
#define LL(x)
google::protobuf::util::TimeUtil::MicrosecondsToTimestamp
static Timestamp MicrosecondsToTimestamp(int64 micros)
Definition: time_util.cc:301
Duration
struct Duration Duration
Definition: php/ext/google/protobuf/protobuf.h:631
time_util.h
googletest.h
EXPECT_FALSE
#define EXPECT_FALSE(cond)
Definition: glog/src/googletest.h:145
google::protobuf::util::TimeUtil::MillisecondsToTimestamp
static Timestamp MillisecondsToTimestamp(int64 millis)
Definition: time_util.cc:307
google::protobuf::util::TimeUtil::ToString
static std::string ToString(const Timestamp &timestamp)
Definition: time_util.cc:156
Duration
Definition: duration.pb.h:69
value
GLsizei const GLfloat * value
Definition: glcorearb.h:3093
google::protobuf::util::TimeUtil::SecondsToDuration
static Duration SecondsToDuration(int64 seconds)
Definition: time_util.cc:258
google::protobuf::util::TimeUtil::DurationToHours
static int64 DurationToHours(const Duration &duration)
Definition: time_util.cc:292
timestamp.pb.h
a
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:3228
google
Definition: data_proto2_to_proto3_util.h:11
google::protobuf::util::TimeUtil::SecondsToTimestamp
static Timestamp SecondsToTimestamp(int64 seconds)
Definition: time_util.cc:313
google::protobuf::util::TimeUtil::TimeTToTimestamp
static Timestamp TimeTToTimestamp(time_t value)
Definition: time_util.cc:335
google::protobuf::util::TimeUtil::TimestampToNanoseconds
static int64 TimestampToNanoseconds(const Timestamp &timestamp)
Definition: time_util.cc:317


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