Skip to content

Commit

Permalink
Fall back to the system clock when building with newlib on a system w…
Browse files Browse the repository at this point in the history
…ithout a monotonic clock.

PiperOrigin-RevId: 493917905
Change-Id: I20137cfcda3671ffc8edcda2b6554aa392e3a00a
  • Loading branch information
thughes authored and copybara-github committed Dec 8, 2022
1 parent d454936 commit 516940f
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions googletest/src/gtest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1123,17 +1123,24 @@ std::string UnitTestImpl::CurrentOsStackTraceExceptTop(int skip_count) {
// A helper class for measuring elapsed times.
class Timer {
public:
Timer() : start_(std::chrono::steady_clock::now()) {}
Timer() : start_(clock::now()) {}

// Return time elapsed in milliseconds since the timer was created.
TimeInMillis Elapsed() {
return std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now() - start_)
clock::now() - start_)
.count();
}

private:
std::chrono::steady_clock::time_point start_;
// Fall back to the system_clock when building with newlib on a system
// without a monotonic clock.
#if defined(_NEWLIB_VERSION) && !defined(CLOCK_MONOTONIC)
using clock = std::chrono::system_clock;
#else
using clock = std::chrono::steady_clock;
#endif
clock::time_point start_;
};

// Returns a timestamp as milliseconds since the epoch. Note this time may jump
Expand Down

0 comments on commit 516940f

Please sign in to comment.