Skip to content

Commit

Permalink
LibTest: Remove uses of gettimeofday in favor of AK::Time
Browse files Browse the repository at this point in the history
gettimeofday is not a thing on Windows or esoteric Unixen.
  • Loading branch information
ADKaster committed Feb 13, 2025
1 parent 88fb2b0 commit 489bea0
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 13 deletions.
7 changes: 3 additions & 4 deletions Libraries/LibTest/TestRunnerUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#pragma once

#include <AK/Time.h>
#include <LibCore/DirIterator.h>
#include <fcntl.h>
#include <sys/stat.h>
Expand All @@ -15,10 +16,8 @@ namespace Test {

inline double get_time_in_ms()
{
struct timeval tv1;
auto return_code = gettimeofday(&tv1, nullptr);
VERIFY(return_code >= 0);
return static_cast<double>(tv1.tv_sec) * 1000.0 + static_cast<double>(tv1.tv_usec) / 1000.0;
auto now = UnixDateTime::now();
return static_cast<double>(now.milliseconds_since_epoch());
}

template<typename Callback>
Expand Down
13 changes: 4 additions & 9 deletions Libraries/LibTest/TestSuite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

#include <AK/Function.h>
#include <AK/Time.h>
#include <LibCore/ArgsParser.h>
#include <LibTest/Macros.h>
#include <LibTest/TestResult.h>
Expand All @@ -22,21 +23,15 @@ class TestElapsedTimer {
public:
TestElapsedTimer() { restart(); }

void restart() { gettimeofday(&m_started, nullptr); }
void restart() { m_started = UnixDateTime::now(); }

u64 elapsed_milliseconds()
{
struct timeval now = {};
gettimeofday(&now, nullptr);

struct timeval delta = {};
timersub(&now, &m_started, &delta);

return delta.tv_sec * 1000 + delta.tv_usec / 1000;
return (UnixDateTime::now() - m_started).to_milliseconds();
}

private:
struct timeval m_started = {};
UnixDateTime m_started;
};

// Declared in Macros.h
Expand Down

0 comments on commit 489bea0

Please sign in to comment.