LibTest: Remove uses of gettimeofday in favor of AK::Time

gettimeofday is not a thing on Windows or esoteric Unixen.
This commit is contained in:
Andrew Kaster 2025-02-12 05:40:29 -07:00 committed by Andrew Kaster
commit 489bea0c23
Notes: github-actions[bot] 2025-02-13 02:14:53 +00:00
2 changed files with 7 additions and 13 deletions

View file

@ -6,6 +6,7 @@
#pragma once #pragma once
#include <AK/Time.h>
#include <LibCore/DirIterator.h> #include <LibCore/DirIterator.h>
#include <fcntl.h> #include <fcntl.h>
#include <sys/stat.h> #include <sys/stat.h>
@ -15,10 +16,8 @@ namespace Test {
inline double get_time_in_ms() inline double get_time_in_ms()
{ {
struct timeval tv1; auto now = UnixDateTime::now();
auto return_code = gettimeofday(&tv1, nullptr); return static_cast<double>(now.milliseconds_since_epoch());
VERIFY(return_code >= 0);
return static_cast<double>(tv1.tv_sec) * 1000.0 + static_cast<double>(tv1.tv_usec) / 1000.0;
} }
template<typename Callback> template<typename Callback>

View file

@ -6,6 +6,7 @@
*/ */
#include <AK/Function.h> #include <AK/Function.h>
#include <AK/Time.h>
#include <LibCore/ArgsParser.h> #include <LibCore/ArgsParser.h>
#include <LibTest/Macros.h> #include <LibTest/Macros.h>
#include <LibTest/TestResult.h> #include <LibTest/TestResult.h>
@ -22,21 +23,15 @@ class TestElapsedTimer {
public: public:
TestElapsedTimer() { restart(); } TestElapsedTimer() { restart(); }
void restart() { gettimeofday(&m_started, nullptr); } void restart() { m_started = UnixDateTime::now(); }
u64 elapsed_milliseconds() u64 elapsed_milliseconds()
{ {
struct timeval now = {}; return (UnixDateTime::now() - m_started).to_milliseconds();
gettimeofday(&now, nullptr);
struct timeval delta = {};
timersub(&now, &m_started, &delta);
return delta.tv_sec * 1000 + delta.tv_usec / 1000;
} }
private: private:
struct timeval m_started = {}; UnixDateTime m_started;
}; };
// Declared in Macros.h // Declared in Macros.h