Skip to content

Commit

Permalink
Googletest export
Browse files Browse the repository at this point in the history
Add millisecond precision to start timestamp in XML/JSON output

- Previous timestamp had format YYYY-MM-DDThh:mm:ss, now YYYY-MM-DDThh:mm:ss.sss
- This conforms to the ISO 8601 standard

PiperOrigin-RevId: 329503623
  • Loading branch information
Abseil Team authored and mbxx committed Sep 1, 2020
1 parent df6b759 commit af1e75c
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 10 deletions.
3 changes: 3 additions & 0 deletions googletest/include/gtest/internal/gtest-string.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ class GTEST_API_ String {
// Formats an int value as "%02d".
static std::string FormatIntWidth2(int value); // "%02d" for width == 2

// Formats an int value to given width with leading zeros.
static std::string FormatIntWidthN(int value, int width);

// Formats an int value as "%X".
static std::string FormatHexInt(int value);

Expand Down
12 changes: 9 additions & 3 deletions googletest/src/gtest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2141,8 +2141,13 @@ bool String::EndsWithCaseInsensitive(

// Formats an int value as "%02d".
std::string String::FormatIntWidth2(int value) {
return FormatIntWidthN(value, 2);
}

// Formats an int value to given width with leading zeros.
std::string String::FormatIntWidthN(int value, int width) {
std::stringstream ss;
ss << std::setfill('0') << std::setw(2) << value;
ss << std::setfill('0') << std::setw(width) << value;
return ss.str();
}

Expand Down Expand Up @@ -4101,13 +4106,14 @@ std::string FormatEpochTimeInMillisAsIso8601(TimeInMillis ms) {
struct tm time_struct;
if (!PortableLocaltime(static_cast<time_t>(ms / 1000), &time_struct))
return "";
// YYYY-MM-DDThh:mm:ss
// YYYY-MM-DDThh:mm:ss.sss
return StreamableToString(time_struct.tm_year + 1900) + "-" +
String::FormatIntWidth2(time_struct.tm_mon + 1) + "-" +
String::FormatIntWidth2(time_struct.tm_mday) + "T" +
String::FormatIntWidth2(time_struct.tm_hour) + ":" +
String::FormatIntWidth2(time_struct.tm_min) + ":" +
String::FormatIntWidth2(time_struct.tm_sec);
String::FormatIntWidth2(time_struct.tm_sec) + "." +
String::FormatIntWidthN(static_cast<int>(ms % 1000), 3);
}

// Streams an XML CDATA section, escaping invalid CDATA sequences as needed.
Expand Down
12 changes: 6 additions & 6 deletions googletest/test/gtest_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -485,28 +485,28 @@ class FormatEpochTimeInMillisAsIso8601Test : public Test {
const TimeInMillis FormatEpochTimeInMillisAsIso8601Test::kMillisPerSec;

TEST_F(FormatEpochTimeInMillisAsIso8601Test, PrintsTwoDigitSegments) {
EXPECT_EQ("2011-10-31T18:52:42",
EXPECT_EQ("2011-10-31T18:52:42.000",
FormatEpochTimeInMillisAsIso8601(1320087162 * kMillisPerSec));
}

TEST_F(FormatEpochTimeInMillisAsIso8601Test, MillisecondsDoNotAffectResult) {
TEST_F(FormatEpochTimeInMillisAsIso8601Test, IncludesMillisecondsAfterDot) {
EXPECT_EQ(
"2011-10-31T18:52:42",
"2011-10-31T18:52:42.234",
FormatEpochTimeInMillisAsIso8601(1320087162 * kMillisPerSec + 234));
}

TEST_F(FormatEpochTimeInMillisAsIso8601Test, PrintsLeadingZeroes) {
EXPECT_EQ("2011-09-03T05:07:02",
EXPECT_EQ("2011-09-03T05:07:02.000",
FormatEpochTimeInMillisAsIso8601(1315026422 * kMillisPerSec));
}

TEST_F(FormatEpochTimeInMillisAsIso8601Test, Prints24HourTime) {
EXPECT_EQ("2011-09-28T17:08:22",
EXPECT_EQ("2011-09-28T17:08:22.000",
FormatEpochTimeInMillisAsIso8601(1317229702 * kMillisPerSec));
}

TEST_F(FormatEpochTimeInMillisAsIso8601Test, PrintsEpochStart) {
EXPECT_EQ("1970-01-01T00:00:00", FormatEpochTimeInMillisAsIso8601(0));
EXPECT_EQ("1970-01-01T00:00:00.000", FormatEpochTimeInMillisAsIso8601(0));
}

# ifdef __BORLANDC__
Expand Down
2 changes: 1 addition & 1 deletion googletest/test/gtest_xml_test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ def NormalizeXml(self, element):

if element.tagName in ('testsuites', 'testsuite', 'testcase'):
timestamp = element.getAttributeNode('timestamp')
timestamp.value = re.sub(r'^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d$',
timestamp.value = re.sub(r'^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d\.\d\d\d$',
'*', timestamp.value)
if element.tagName in ('testsuites', 'testsuite', 'testcase'):
time = element.getAttributeNode('time')
Expand Down

0 comments on commit af1e75c

Please sign in to comment.