time_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.
31 
33 #include <gtest/gtest.h>
34 
35 namespace google {
36 namespace protobuf {
37 namespace internal {
38 namespace {
39 static const int64 kSecondsPerDay = 3600 * 24;
40 
41 // For DateTime, tests will mostly focuse on the date part because that's
42 // the tricky one.
43 int64 CreateTimestamp(int year, int month, int day) {
44  DateTime time;
45  time.year = year;
46  time.month = month;
47  time.day = day;
48  time.hour = time.minute = time.second = 0;
49  int64 result;
50  GOOGLE_CHECK(DateTimeToSeconds(time, &result));
51  // Check that a roundtrip produces the same result.
52  GOOGLE_CHECK(SecondsToDateTime(result, &time));
53  GOOGLE_CHECK(time.year == year);
54  GOOGLE_CHECK(time.month == month);
55  GOOGLE_CHECK(time.day == day);
56  return result;
57 }
58 
59 TEST(DateTimeTest, SimpleTime) {
60  DateTime time;
61  ASSERT_TRUE(SecondsToDateTime(1, &time));
62  EXPECT_EQ(1970, time.year);
63  EXPECT_EQ(1, time.month);
64  EXPECT_EQ(1, time.day);
65  EXPECT_EQ(0, time.hour);
66  EXPECT_EQ(0, time.minute);
67  EXPECT_EQ(1, time.second);
68  int64 seconds;
69  ASSERT_TRUE(DateTimeToSeconds(time, &seconds));
70  EXPECT_EQ(1, seconds);
71 
72  ASSERT_TRUE(SecondsToDateTime(-1, &time));
73  EXPECT_EQ(1969, time.year);
74  EXPECT_EQ(12, time.month);
75  EXPECT_EQ(31, time.day);
76  EXPECT_EQ(23, time.hour);
77  EXPECT_EQ(59, time.minute);
78  EXPECT_EQ(59, time.second);
79  ASSERT_TRUE(DateTimeToSeconds(time, &seconds));
80  EXPECT_EQ(-1, seconds);
81 
82  DateTime start, end;
83  start.year = 1;
84  start.month = 1;
85  start.day = 1;
86  start.hour = 0;
87  start.minute = 0;
88  start.second = 0;
89  end.year = 9999;
90  end.month = 12;
91  end.day = 31;
92  end.hour = 23;
93  end.minute = 59;
94  end.second = 59;
95  int64 start_time, end_time;
96  ASSERT_TRUE(DateTimeToSeconds(start, &start_time));
97  ASSERT_TRUE(DateTimeToSeconds(end, &end_time));
98  EXPECT_EQ(315537897599LL, end_time - start_time);
99  ASSERT_TRUE(SecondsToDateTime(start_time, &time));
100  ASSERT_TRUE(DateTimeToSeconds(time, &seconds));
101  EXPECT_EQ(start_time, seconds);
102  ASSERT_TRUE(SecondsToDateTime(end_time, &time));
103  ASSERT_TRUE(DateTimeToSeconds(time, &seconds));
104  EXPECT_EQ(end_time, seconds);
105 }
106 
107 TEST(DateTimeTest, DayInMonths) {
108  // Check that month boundaries are handled correctly.
109  EXPECT_EQ(kSecondsPerDay,
110  CreateTimestamp(2015, 1, 1) - CreateTimestamp(2014, 12, 31));
111  EXPECT_EQ(kSecondsPerDay,
112  CreateTimestamp(2015, 2, 1) - CreateTimestamp(2015, 1, 31));
113  EXPECT_EQ(kSecondsPerDay,
114  CreateTimestamp(2015, 3, 1) - CreateTimestamp(2015, 2, 28));
115  EXPECT_EQ(kSecondsPerDay,
116  CreateTimestamp(2015, 4, 1) - CreateTimestamp(2015, 3, 31));
117  EXPECT_EQ(kSecondsPerDay,
118  CreateTimestamp(2015, 5, 1) - CreateTimestamp(2015, 4, 30));
119  EXPECT_EQ(kSecondsPerDay,
120  CreateTimestamp(2015, 6, 1) - CreateTimestamp(2015, 5, 31));
121  EXPECT_EQ(kSecondsPerDay,
122  CreateTimestamp(2015, 7, 1) - CreateTimestamp(2015, 6, 30));
123  EXPECT_EQ(kSecondsPerDay,
124  CreateTimestamp(2015, 8, 1) - CreateTimestamp(2015, 7, 31));
125  EXPECT_EQ(kSecondsPerDay,
126  CreateTimestamp(2015, 9, 1) - CreateTimestamp(2015, 8, 31));
127  EXPECT_EQ(kSecondsPerDay,
128  CreateTimestamp(2015, 10, 1) - CreateTimestamp(2015, 9, 30));
129  EXPECT_EQ(kSecondsPerDay,
130  CreateTimestamp(2015, 11, 1) - CreateTimestamp(2015, 10, 31));
131  EXPECT_EQ(kSecondsPerDay,
132  CreateTimestamp(2015, 12, 1) - CreateTimestamp(2015, 11, 30));
133  EXPECT_EQ(kSecondsPerDay,
134  CreateTimestamp(2016, 1, 1) - CreateTimestamp(2015, 12, 31));
135 }
136 
137 TEST(DateTimeTest, LeapYear) {
138  // Non-leap year.
139  EXPECT_EQ(kSecondsPerDay,
140  CreateTimestamp(2015, 3, 1) - CreateTimestamp(2015, 2, 28));
141  // Leap year.
142  EXPECT_EQ(kSecondsPerDay,
143  CreateTimestamp(2016, 3, 1) - CreateTimestamp(2016, 2, 29));
144  // Non-leap year.
145  EXPECT_EQ(kSecondsPerDay,
146  CreateTimestamp(2100, 3, 1) - CreateTimestamp(2100, 2, 28));
147  // Leap year.
148  EXPECT_EQ(kSecondsPerDay,
149  CreateTimestamp(2400, 3, 1) - CreateTimestamp(2400, 2, 29));
150 }
151 
152 TEST(DateTimeTest, WrongDays) {
153  int64 seconds;
154  DateTime time;
155  time.hour = 0;
156  time.minute = 0;
157  time.second = 0;
158  time.month = 2;
159 
160  // Non-leap year.
161  time.year = 2015;
162  time.day = 29;
163  ASSERT_FALSE(DateTimeToSeconds(time, &seconds));
164 
165  // Leap year.
166  time.year = 2016;
167  time.day = 29;
168  ASSERT_TRUE(DateTimeToSeconds(time, &seconds));
169  time.day = 30;
170  ASSERT_FALSE(DateTimeToSeconds(time, &seconds));
171 
172  // Non-leap year.
173  time.year = 2100;
174  time.day = 29;
175  ASSERT_FALSE(DateTimeToSeconds(time, &seconds));
176 
177  // Leap year.
178  time.year = 2400;
179  time.day = 29;
180  ASSERT_TRUE(DateTimeToSeconds(time, &seconds));
181  time.day = 30;
182  ASSERT_FALSE(DateTimeToSeconds(time, &seconds));
183 
184  // Non-february
185  time.year = 2015;
186  time.month = 1;
187  time.day = 0;
188  ASSERT_FALSE(DateTimeToSeconds(time, &seconds));
189  time.day = 1;
190  ASSERT_TRUE(DateTimeToSeconds(time, &seconds));
191  time.day = 31;
192  ASSERT_TRUE(DateTimeToSeconds(time, &seconds));
193  time.day = 32;
194  ASSERT_FALSE(DateTimeToSeconds(time, &seconds));
195 
196  // Bad month
197  time.year = 2015;
198  time.month = 0;
199  time.day = 1;
200  ASSERT_FALSE(DateTimeToSeconds(time, &seconds));
201  time.month = 13;
202  ASSERT_FALSE(DateTimeToSeconds(time, &seconds));
203 }
204 
205 TEST(DateTimeTest, StringFormat) {
206  DateTime start, end;
207  start.year = 1;
208  start.month = 1;
209  start.day = 1;
210  start.hour = 0;
211  start.minute = 0;
212  start.second = 0;
213  end.year = 9999;
214  end.month = 12;
215  end.day = 31;
216  end.hour = 23;
217  end.minute = 59;
218  end.second = 59;
219  int64 start_time, end_time;
220  ASSERT_TRUE(DateTimeToSeconds(start, &start_time));
221  ASSERT_TRUE(DateTimeToSeconds(end, &end_time));
222 
223  EXPECT_EQ("0001-01-01T00:00:00Z", FormatTime(start_time, 0));
224  EXPECT_EQ("9999-12-31T23:59:59Z", FormatTime(end_time, 0));
225 
226  // Make sure the nanoseconds part is formated correctly.
227  EXPECT_EQ("1970-01-01T00:00:00.010Z", FormatTime(0, 10000000));
228  EXPECT_EQ("1970-01-01T00:00:00.000010Z", FormatTime(0, 10000));
229  EXPECT_EQ("1970-01-01T00:00:00.000000010Z", FormatTime(0, 10));
230 }
231 
232 TEST(DateTimeTest, ParseString) {
233  int64 seconds;
234  int32 nanos;
235  ASSERT_TRUE(ParseTime("0001-01-01T00:00:00Z", &seconds, &nanos));
236  EXPECT_EQ("0001-01-01T00:00:00Z", FormatTime(seconds, nanos));
237  ASSERT_TRUE(ParseTime("9999-12-31T23:59:59.999999999Z", &seconds, &nanos));
238  EXPECT_EQ("9999-12-31T23:59:59.999999999Z", FormatTime(seconds, nanos));
239 
240  // Test time zone offsets.
241  ASSERT_TRUE(ParseTime("1970-01-01T00:00:00-08:00", &seconds, &nanos));
242  EXPECT_EQ("1970-01-01T08:00:00Z", FormatTime(seconds, nanos));
243  ASSERT_TRUE(ParseTime("1970-01-01T00:00:00+08:00", &seconds, &nanos));
244  EXPECT_EQ("1969-12-31T16:00:00Z", FormatTime(seconds, nanos));
245 
246  // Test nanoseconds.
247  ASSERT_TRUE(ParseTime("1970-01-01T00:00:00.01Z", &seconds, &nanos));
248  EXPECT_EQ("1970-01-01T00:00:00.010Z", FormatTime(seconds, nanos));
249  ASSERT_TRUE(ParseTime("1970-01-01T00:00:00.00001-08:00", &seconds, &nanos));
250  EXPECT_EQ("1970-01-01T08:00:00.000010Z", FormatTime(seconds, nanos));
251  ASSERT_TRUE(ParseTime("1970-01-01T00:00:00.00000001+08:00", &seconds, &nanos));
252  EXPECT_EQ("1969-12-31T16:00:00.000000010Z", FormatTime(seconds, nanos));
253  // Fractional parts less than 1 nanosecond will be ignored.
254  ASSERT_TRUE(ParseTime("1970-01-01T00:00:00.0123456789Z", &seconds, &nanos));
255  EXPECT_EQ("1970-01-01T00:00:00.012345678Z", FormatTime(seconds, nanos));
256 }
257 
258 } // namespace
259 } // namespace internal
260 } // namespace protobuf
261 } // namespace google
ASSERT_FALSE
#define ASSERT_FALSE(condition)
Definition: gtest.h:1998
end
GLuint GLuint end
Definition: glcorearb.h:2858
google::protobuf::int64
int64_t int64
Definition: protobuf/src/google/protobuf/stubs/port.h:151
gtest.h
EXPECT_EQ
#define EXPECT_EQ(val1, val2)
Definition: glog/src/googletest.h:155
google::protobuf::int32
int32_t int32
Definition: protobuf/src/google/protobuf/stubs/port.h:150
google::protobuf.internal::DateTimeToSeconds
bool DateTimeToSeconds(const DateTime &time, int64 *seconds)
Definition: time.cc:256
start
GLuint start
Definition: glcorearb.h:2858
time.h
google::protobuf.internal::ParseTime
bool ParseTime(const string &value, int64 *seconds, int32 *nanos)
Definition: time.cc:285
ASSERT_TRUE
#define ASSERT_TRUE(condition)
Definition: gtest.h:1995
GOOGLE_CHECK
#define GOOGLE_CHECK(EXPRESSION)
Definition: logging.h:153
LL
#define LL(x)
googletest.h
google::protobuf.internal::TEST
TEST(GeneratedMapFieldTest, Accessors)
Definition: src/google/protobuf/map_test.cc:1837
google::protobuf.internal::FormatTime
string FormatTime(int64 seconds, int32 nanos)
Definition: time.cc:271
internal
Definition: any.pb.h:40
google
Definition: data_proto2_to_proto3_util.h:11
google::protobuf.internal::SecondsToDateTime
bool SecondsToDateTime(int64 seconds, DateTime *time)
Definition: time.cc:211


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