serenity/Userland/Libraries/LibCore/ElapsedTimer.cpp
Andrew Kaster 4afa6e264c LibCore+LibWeb: Use AK::Time instead of timeval in Core::ElapsedTimer
This removes the direct dependency on sys/time.h from ElapsedTimer, and
makes the code a lot cleaner by using the helpers from AK::Time for
time math and getting the current timestamp.
2023-01-07 14:51:04 +01:00

45 lines
806 B
C++

/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Assertions.h>
#include <AK/Time.h>
#include <LibCore/ElapsedTimer.h>
namespace Core {
ElapsedTimer ElapsedTimer::start_new()
{
ElapsedTimer timer;
timer.start();
return timer;
}
void ElapsedTimer::start()
{
m_valid = true;
m_origin_time = m_precise ? Time::now_monotonic() : Time::now_monotonic_coarse();
}
void ElapsedTimer::reset()
{
m_valid = false;
m_origin_time = {};
}
i64 ElapsedTimer::elapsed() const
{
return elapsed_time().to_milliseconds();
}
Time ElapsedTimer::elapsed_time() const
{
VERIFY(is_valid());
auto now = m_precise ? Time::now_monotonic() : Time::now_monotonic_coarse();
return now - m_origin_time;
}
}