summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibCore/ElapsedTimer.cpp
blob: 93036780f265bb5e08510e8f1b442e23428b1a10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*
 * 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 ? Duration::now_monotonic() : Duration::now_monotonic_coarse();
}

void ElapsedTimer::reset()
{
    m_valid = false;
    m_origin_time = {};
}

i64 ElapsedTimer::elapsed_milliseconds() const
{
    return elapsed_time().to_milliseconds();
}

Duration ElapsedTimer::elapsed_time() const
{
    VERIFY(is_valid());
    auto now = m_precise ? Duration::now_monotonic() : Duration::now_monotonic_coarse();
    return now - m_origin_time;
}

}