mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 06:47:35 +00:00
AK: Add Time::now_<clock_id> functions for obtaining the current time
In the quest of removing as timespec / timeval usage in the Userland as possible, we need a way to conveniently retrieving the current clock time from the kernel and storing it in `AK::Time` format.
This commit is contained in:
parent
188e5f018f
commit
dae17ce7e3
2 changed files with 39 additions and 0 deletions
32
AK/Time.cpp
32
AK/Time.cpp
|
@ -14,6 +14,7 @@
|
|||
# include <Kernel/UnixTypes.h>
|
||||
#else
|
||||
# include <sys/time.h>
|
||||
# include <time.h>
|
||||
#endif
|
||||
|
||||
namespace AK {
|
||||
|
@ -288,4 +289,35 @@ Time Time::from_half_sanitized(i64 seconds, i32 extra_seconds, u32 nanoseconds)
|
|||
return Time { seconds + extra_seconds, nanoseconds };
|
||||
}
|
||||
|
||||
#ifndef KERNEL
|
||||
namespace {
|
||||
static Time now_time_from_clock(clockid_t clock_id)
|
||||
{
|
||||
timespec now_spec {};
|
||||
::clock_gettime(clock_id, &now_spec);
|
||||
return Time::from_timespec(now_spec);
|
||||
}
|
||||
}
|
||||
Time Time::now_realtime()
|
||||
{
|
||||
return now_time_from_clock(CLOCK_REALTIME);
|
||||
}
|
||||
|
||||
Time Time::now_realtime_coarse()
|
||||
{
|
||||
return now_time_from_clock(CLOCK_REALTIME_COARSE);
|
||||
}
|
||||
|
||||
Time Time::now_monotonic()
|
||||
{
|
||||
return now_time_from_clock(CLOCK_MONOTONIC);
|
||||
}
|
||||
|
||||
Time Time::now_monotonic_coarse()
|
||||
{
|
||||
return now_time_from_clock(CLOCK_MONOTONIC_COARSE);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue