131 time_t expected_ts = time(NULL); |
131 time_t expected_ts = time(NULL); |
132 |
132 |
133 // Verify format |
133 // Verify format |
134 int y, M, d, h, m; |
134 int y, M, d, h, m; |
135 double s; |
135 double s; |
136 int read = sscanf(timestr, "%d-%d-%dT%d:%d:%lfZ", &y, &M, &d, &h, &m, &s); |
136 int read = sscanf(timestr, "%d-%d-%dT%d:%d:%lf", &y, &M, &d, &h, &m, &s); |
137 ASSERT_EQ(6, read); |
137 ASSERT_EQ(6, read) << "Invalid format: " << timestr; |
138 |
138 |
139 // Verify reported time & date |
139 // Verify reported time & date |
140 struct tm reported_time = {0}; |
140 struct tm reported_time = {0}; |
141 reported_time.tm_year = y - 1900; |
141 reported_time.tm_year = y - 1900; |
142 reported_time.tm_mon = M - 1; |
142 reported_time.tm_mon = M - 1; |
154 // Allow up to 10 seconds in difference |
154 // Allow up to 10 seconds in difference |
155 ASSERT_LE(diff, 10) << "Reported time: " << reported_ts << " (" << timestr << ")" |
155 ASSERT_LE(diff, 10) << "Reported time: " << reported_ts << " (" << timestr << ")" |
156 << ", expected time: " << expected_ts; |
156 << ", expected time: " << expected_ts; |
157 } |
157 } |
158 |
158 |
|
159 // Test the utctime decoration |
|
160 TEST(LogDecorations, iso8601_utctime) { |
|
161 LogDecorators decorator_selection; |
|
162 ASSERT_TRUE(decorator_selection.parse("utctime")); |
|
163 LogDecorations decorations(LogLevel::Info, tagset, decorator_selection); |
|
164 |
|
165 const char *timestr = decorations.decoration(LogDecorators::utctime_decorator); |
|
166 time_t expected_ts = time(NULL); |
|
167 |
|
168 // Verify format |
|
169 char trailing_character; |
|
170 int y, M, d, h, m, offset; |
|
171 double s; |
|
172 int read = sscanf(timestr, "%d-%d-%dT%d:%d:%lf%c%d", &y, &M, &d, &h, &m, &s, &trailing_character, &offset); |
|
173 ASSERT_GT(read, 7) << "Invalid format: " << timestr; |
|
174 |
|
175 // Ensure time is UTC (no offset) |
|
176 if (trailing_character == '+') { |
|
177 ASSERT_EQ(0, offset) << "Invalid offset: " << timestr; |
|
178 } else { |
|
179 ASSERT_EQ('Z', trailing_character) << "Invalid offset: " << timestr; |
|
180 } |
|
181 |
|
182 struct tm reported_time = {0}; |
|
183 reported_time.tm_year = y - 1900; |
|
184 reported_time.tm_mon = M - 1; |
|
185 reported_time.tm_mday = d; |
|
186 reported_time.tm_hour = h; |
|
187 reported_time.tm_min = m; |
|
188 reported_time.tm_sec = s; |
|
189 reported_time.tm_isdst = 0; // No DST for UTC timestamps |
|
190 time_t reported_ts = mktime(&reported_time); |
|
191 expected_ts = mktime(gmtime(&expected_ts)); |
|
192 time_t diff = reported_ts - expected_ts; |
|
193 if (diff < 0) { |
|
194 diff = -diff; |
|
195 } |
|
196 // Allow up to 10 seconds in difference |
|
197 ASSERT_LE(diff, 10) << "Reported time: " << reported_ts << " (" << timestr << ")" |
|
198 << ", expected time: " << expected_ts; |
|
199 } |
|
200 |
159 // Test the pid and tid decorations |
201 // Test the pid and tid decorations |
160 TEST(LogDecorations, identifiers) { |
202 TEST(LogDecorations, identifiers) { |
161 LogDecorators decorator_selection; |
203 LogDecorators decorator_selection; |
162 ASSERT_TRUE(decorator_selection.parse("pid,tid")); |
204 ASSERT_TRUE(decorator_selection.parse("pid,tid")); |
163 LogDecorations decorations(LogLevel::Info, tagset, decorator_selection); |
205 LogDecorations decorations(LogLevel::Info, tagset, decorator_selection); |