#include <softbus_core/time/HwClock.h>

#include <chrono>

#ifdef SOFTBUS_PLATFORM_WINDOWS
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>
#endif

namespace softbus::core {

int64_t HwClock::nowNs()
{
#ifdef SOFTBUS_PLATFORM_WINDOWS
    static LARGE_INTEGER frequency = [] {
        LARGE_INTEGER f{};
        QueryPerformanceFrequency(&f);
        return f;
    }();
    LARGE_INTEGER counter{};
    QueryPerformanceCounter(&counter);
    return static_cast<int64_t>((counter.QuadPart * 1'000'000'000LL) / frequency.QuadPart);
#else
    timespec ts{};
    clock_gettime(CLOCK_REALTIME, &ts);
    return static_cast<int64_t>(ts.tv_sec) * 1'000'000'000LL + ts.tv_nsec;
#endif
}

double HwClock::nsToMs(int64_t ns)
{
    return static_cast<double>(ns) / 1'000'000.0;
}

} // namespace softbus::core
