Add TimevalToTimespec and use it to get CurrentTime on macOS
A follow-up to: https://chromium-review.googlesource.com/c/crashpad/crashpad/+/744298/35/util/file/filesystem_test.cc#31 Bug: crashpad:206 Change-Id: I2f711839ae7746104f1fee9685f2ab9059d9c2b3 Reviewed-on: https://chromium-review.googlesource.com/762479 Commit-Queue: Joshua Peraza <jperaza@chromium.org> Reviewed-by: Mark Mentovai <mark@chromium.org>
This commit is contained in:
Родитель
0e3c38a4ca
Коммит
b8f61fdc8b
|
@ -14,6 +14,8 @@
|
|||
|
||||
#include "util/file/filesystem.h"
|
||||
|
||||
#include <sys/time.h>
|
||||
|
||||
#include "base/logging.h"
|
||||
#include "build/build_config.h"
|
||||
#include "gtest/gtest.h"
|
||||
|
@ -21,13 +23,23 @@
|
|||
#include "test/filesystem.h"
|
||||
#include "test/gtest_disabled.h"
|
||||
#include "test/scoped_temp_dir.h"
|
||||
#include "util/misc/time.h"
|
||||
|
||||
namespace crashpad {
|
||||
namespace test {
|
||||
namespace {
|
||||
|
||||
bool CurrentTime(timespec* now) {
|
||||
#if defined(OS_POSIX)
|
||||
#if defined(OS_MACOSX)
|
||||
timeval now_tv;
|
||||
int res = gettimeofday(&now_tv, nullptr);
|
||||
if (res != 0) {
|
||||
EXPECT_EQ(res, 0) << ErrnoMessage("gettimeofday");
|
||||
return false;
|
||||
}
|
||||
TimevalToTimespec(now_tv, now);
|
||||
return true;
|
||||
#elif defined(OS_POSIX)
|
||||
int res = clock_gettime(CLOCK_REALTIME, now);
|
||||
if (res != 0) {
|
||||
EXPECT_EQ(res, 0) << ErrnoMessage("clock_gettime");
|
||||
|
|
|
@ -46,4 +46,9 @@ bool TimespecToTimeval(const timespec& ts, timeval* tv) {
|
|||
return AssignIfInRange(&tv->tv_sec, ts.tv_sec);
|
||||
}
|
||||
|
||||
void TimevalToTimespec(const timeval& tv, timespec* ts) {
|
||||
ts->tv_sec = tv.tv_sec;
|
||||
ts->tv_nsec = tv.tv_usec * 1000;
|
||||
}
|
||||
|
||||
} // namespace crashpad
|
||||
|
|
|
@ -42,6 +42,9 @@ void SubtractTimespec(const timespec& ts1,
|
|||
//! \return `true` if the assignment is possible without truncation.
|
||||
bool TimespecToTimeval(const timespec& ts, timeval* tv);
|
||||
|
||||
//! \brief Convert the timeval \a tv to a timespec \a ts.
|
||||
void TimevalToTimespec(const timeval& tv, timespec* ts);
|
||||
|
||||
#if defined(OS_WIN) || DOXYGEN
|
||||
|
||||
//! \brief Convert a `timespec` to a Windows `FILETIME`, converting from POSIX
|
||||
|
|
|
@ -52,6 +52,12 @@ TEST(Time, TimeConversions) {
|
|||
EXPECT_EQ(timeval_birthdate.tv_sec, kCrashpadBirthdate.tv_sec);
|
||||
EXPECT_EQ(timeval_birthdate.tv_usec, kCrashpadBirthdate.tv_nsec / 1000);
|
||||
|
||||
timespec timespec_birthdate;
|
||||
TimevalToTimespec(timeval_birthdate, ×pec_birthdate);
|
||||
EXPECT_EQ(timespec_birthdate.tv_sec, kCrashpadBirthdate.tv_sec);
|
||||
EXPECT_EQ(timespec_birthdate.tv_nsec,
|
||||
kCrashpadBirthdate.tv_nsec - (kCrashpadBirthdate.tv_nsec % 1000));
|
||||
|
||||
constexpr timespec kEndOfTime = {
|
||||
/* .tv_sec= */ std::numeric_limits<decltype(timespec::tv_sec)>::max(),
|
||||
/* .tv_nsec= */ 0
|
||||
|
@ -75,7 +81,7 @@ TEST(Time, TimeConversions) {
|
|||
EXPECT_EQ(filetime.dwLowDateTime, filetime_birthdate.dwLowDateTime);
|
||||
EXPECT_EQ(filetime.dwHighDateTime, filetime_birthdate.dwHighDateTime);
|
||||
|
||||
timespec timespec_birthdate = FiletimeToTimespecEpoch(filetime_birthdate);
|
||||
timespec_birthdate = FiletimeToTimespecEpoch(filetime_birthdate);
|
||||
EXPECT_EQ(timespec_birthdate.tv_sec, kCrashpadBirthdate.tv_sec);
|
||||
EXPECT_EQ(timespec_birthdate.tv_nsec,
|
||||
kCrashpadBirthdate.tv_nsec - kCrashpadBirthdate.tv_nsec % 100);
|
||||
|
|
Загрузка…
Ссылка в новой задаче