hotspot/test/native/logging/test_logDecorations.cpp
changeset 42066 46f6db750b17
parent 41705 332239c052cc
child 42612 87ca7feb68bf
--- a/hotspot/test/native/logging/test_logDecorations.cpp	Thu Oct 13 14:49:34 2016 +0200
+++ b/hotspot/test/native/logging/test_logDecorations.cpp	Fri Oct 21 10:18:11 2016 +0200
@@ -133,8 +133,8 @@
   // Verify format
   int y, M, d, h, m;
   double s;
-  int read = sscanf(timestr, "%d-%d-%dT%d:%d:%lfZ", &y, &M, &d, &h, &m, &s);
-  ASSERT_EQ(6, read);
+  int read = sscanf(timestr, "%d-%d-%dT%d:%d:%lf", &y, &M, &d, &h, &m, &s);
+  ASSERT_EQ(6, read) << "Invalid format: " << timestr;
 
   // Verify reported time & date
   struct tm reported_time = {0};
@@ -156,6 +156,48 @@
       << ", expected time: " << expected_ts;
 }
 
+// Test the utctime decoration
+TEST(LogDecorations, iso8601_utctime) {
+  LogDecorators decorator_selection;
+  ASSERT_TRUE(decorator_selection.parse("utctime"));
+  LogDecorations decorations(LogLevel::Info, tagset, decorator_selection);
+
+  const char *timestr = decorations.decoration(LogDecorators::utctime_decorator);
+  time_t expected_ts = time(NULL);
+
+  // Verify format
+  char trailing_character;
+  int y, M, d, h, m, offset;
+  double s;
+  int read = sscanf(timestr, "%d-%d-%dT%d:%d:%lf%c%d", &y, &M, &d, &h, &m, &s, &trailing_character, &offset);
+  ASSERT_GT(read, 7) << "Invalid format: " << timestr;
+
+  // Ensure time is UTC (no offset)
+  if (trailing_character == '+') {
+    ASSERT_EQ(0, offset) << "Invalid offset: " << timestr;
+  } else {
+    ASSERT_EQ('Z', trailing_character) << "Invalid offset: " << timestr;
+  }
+
+  struct tm reported_time = {0};
+  reported_time.tm_year = y - 1900;
+  reported_time.tm_mon = M - 1;
+  reported_time.tm_mday = d;
+  reported_time.tm_hour = h;
+  reported_time.tm_min = m;
+  reported_time.tm_sec = s;
+  reported_time.tm_isdst = 0; // No DST for UTC timestamps
+  time_t reported_ts = mktime(&reported_time);
+  expected_ts = mktime(gmtime(&expected_ts));
+  time_t diff = reported_ts - expected_ts;
+  if (diff < 0) {
+    diff = -diff;
+  }
+  // Allow up to 10 seconds in difference
+  ASSERT_LE(diff, 10) << "Reported time: " << reported_ts << " (" << timestr << ")"
+      << ", expected time: " << expected_ts;
+}
+
 // Test the pid and tid decorations
 TEST(LogDecorations, identifiers) {
   LogDecorators decorator_selection;