mirror of
https://github.com/thunderbrewhq/common.git
synced 2025-12-12 11:12:29 +00:00
feat(time): add common time functions
This commit is contained in:
parent
d1bfb1394e
commit
1da79e35bf
2 changed files with 65 additions and 0 deletions
53
common/Time.cpp
Normal file
53
common/Time.cpp
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
#include "common/Time.hpp"
|
||||
|
||||
#if defined(WHOA_SYSTEM_WIN)
|
||||
#include <windows.h>
|
||||
#elif defined(WHOA_SYSTEM_MAC)
|
||||
#include <mach/mach_time.h>
|
||||
#include <unistd.h>
|
||||
#elif defined(WHOA_SYSTEM_LINUX)
|
||||
#include <chrono>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
uint64_t OsGetAsyncTimeMs() {
|
||||
#if defined(WHOA_SYSTEM_WIN)
|
||||
return GetTickCount();
|
||||
|
||||
#elif defined(WHOA_SYSTEM_MAC)
|
||||
static mach_timebase_info_data_t timebase;
|
||||
|
||||
if (timebase.denom == 0) {
|
||||
mach_timebase_info(&timebase);
|
||||
}
|
||||
|
||||
uint64_t ticks = mach_absolute_time();
|
||||
|
||||
return ticks * (timebase.numer / timebase.denom) / 1000000;
|
||||
|
||||
#elif defined(WHOA_SYSTEM_LINUX)
|
||||
auto now = std::chrono::steady_clock::now();
|
||||
uint64_t ticks = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count();
|
||||
return ticks;
|
||||
#endif
|
||||
}
|
||||
|
||||
uint64_t OsGetAsyncTimeMsPrecise() {
|
||||
#if defined(WHOA_SYSTEM_WIN)
|
||||
// TODO QueryPerformanceCounter implementation
|
||||
return OsGetAsyncTimeMs();
|
||||
|
||||
#else
|
||||
return OsGetAsyncTimeMs();
|
||||
#endif
|
||||
}
|
||||
|
||||
void OsSleep(uint32_t duration) {
|
||||
#if defined(WHOA_SYSTEM_WIN)
|
||||
Sleep(duration);
|
||||
#endif
|
||||
|
||||
#if defined(WHOA_SYSTEM_MAC) || defined(WHOA_SYSTEM_LINUX)
|
||||
usleep(duration);
|
||||
#endif
|
||||
}
|
||||
12
common/Time.hpp
Normal file
12
common/Time.hpp
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
#ifndef COMMON_TIME_HPP
|
||||
#define COMMON_TIME_HPP
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
uint64_t OsGetAsyncTimeMs();
|
||||
|
||||
uint64_t OsGetAsyncTimeMsPrecise();
|
||||
|
||||
void OsSleep(uint32_t duration);
|
||||
|
||||
#endif
|
||||
Loading…
Add table
Add a link
Reference in a new issue