#include "chrono"
#include "cerrno"
#include "system_error"
#include <time.h>
#include "include/apple_availability.h"
#if __has_include(<unistd.h>)
# include <unistd.h>
#endif
#if __has_include(<sys/time.h>)
# include <sys/time.h>
#endif
#if !defined(__APPLE__) && _POSIX_TIMERS > 0
# define _LIBCPP_USE_CLOCK_GETTIME
#endif
#if defined(_LIBCPP_WIN32API)
# define WIN32_LEAN_AND_MEAN
# define VC_EXTRA_LEAN
# include <windows.h>
# if _WIN32_WINNT >= _WIN32_WINNT_WIN8
# include <winapifamily.h>
# endif
#endif
#if __has_include(<mach/mach_time.h>)
# include <mach/mach_time.h>
#endif
#if defined(__ELF__) && defined(_LIBCPP_LINK_RT_LIB)
# pragma comment(lib, "rt")
#endif
_LIBCPP_BEGIN_NAMESPACE_STD
namespace chrono
{
#if defined(_LIBCPP_WIN32API)
static system_clock::time_point __libcpp_system_clock_now() {
using filetime_duration =
_VSTD::chrono::duration<__int64,
_VSTD::ratio_multiply<_VSTD::ratio<100, 1>,
nanoseconds::period>>;
static _LIBCPP_CONSTEXPR const seconds nt_to_unix_epoch{11644473600};
FILETIME ft;
#if _WIN32_WINNT >= _WIN32_WINNT_WIN8 && WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
GetSystemTimePreciseAsFileTime(&ft);
#else
GetSystemTimeAsFileTime(&ft);
#endif
filetime_duration d{(static_cast<__int64>(ft.dwHighDateTime) << 32) |
static_cast<__int64>(ft.dwLowDateTime)};
return system_clock::time_point(duration_cast<system_clock::duration>(d - nt_to_unix_epoch));
}
#elif defined(CLOCK_REALTIME) && defined(_LIBCPP_USE_CLOCK_GETTIME)
static system_clock::time_point __libcpp_system_clock_now() {
struct timespec tp;
if (0 != clock_gettime(CLOCK_REALTIME, &tp))
__throw_system_error(errno, "clock_gettime(CLOCK_REALTIME) failed");
return system_clock::time_point(seconds(tp.tv_sec) + microseconds(tp.tv_nsec / 1000));
}
#else
static system_clock::time_point __libcpp_system_clock_now() {
timeval tv;
gettimeofday(&tv, 0);
return system_clock::time_point(seconds(tv.tv_sec) + microseconds(tv.tv_usec));
}
#endif
const bool system_clock::is_steady;
system_clock::time_point
system_clock::now() _NOEXCEPT
{
return __libcpp_system_clock_now();
}
time_t
system_clock::to_time_t(const time_point& t) _NOEXCEPT
{
return time_t(duration_cast<seconds>(t.time_since_epoch()).count());
}
system_clock::time_point
system_clock::from_time_t(time_t t) _NOEXCEPT
{
return system_clock::time_point(seconds(t));
}
#ifndef _LIBCPP_HAS_NO_MONOTONIC_CLOCK
#if defined(__APPLE__)
#if (defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 101200) || \
(defined(__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__ < 100000) || \
(defined(__ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__ < 100000) || \
(defined(__ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__ < 30000)
# define _LIBCPP_USE_OLD_MACH_ABSOLUTE_TIME
#endif
#if defined(_LIBCPP_USE_OLD_MACH_ABSOLUTE_TIME)
static steady_clock::rep steady_simplified() {
return static_cast<steady_clock::rep>(mach_absolute_time());
}
static double compute_steady_factor() {
mach_timebase_info_data_t MachInfo;
mach_timebase_info(&MachInfo);
return static_cast<double>(MachInfo.numer) / MachInfo.denom;
}
static steady_clock::rep steady_full() {
static const double factor = compute_steady_factor();
return static_cast<steady_clock::rep>(mach_absolute_time() * factor);
}
typedef steady_clock::rep (*FP)();
static FP init_steady_clock() {
mach_timebase_info_data_t MachInfo;
mach_timebase_info(&MachInfo);
if (MachInfo.numer == MachInfo.denom)
return &steady_simplified;
return &steady_full;
}
static steady_clock::time_point __libcpp_steady_clock_now() {
static FP fp = init_steady_clock();
return steady_clock::time_point(steady_clock::duration(fp()));
}
#else
static steady_clock::time_point __libcpp_steady_clock_now() {
struct timespec tp;
if (0 != clock_gettime(CLOCK_MONOTONIC_RAW, &tp))
__throw_system_error(errno, "clock_gettime(CLOCK_MONOTONIC_RAW) failed");
return steady_clock::time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec));
}
#endif
#elif defined(_LIBCPP_WIN32API)
static LARGE_INTEGER
__QueryPerformanceFrequency()
{
LARGE_INTEGER val;
(void) QueryPerformanceFrequency(&val);
return val;
}
static steady_clock::time_point __libcpp_steady_clock_now() {
static const LARGE_INTEGER freq = __QueryPerformanceFrequency();
LARGE_INTEGER counter;
(void) QueryPerformanceCounter(&counter);
auto seconds = counter.QuadPart / freq.QuadPart;
auto fractions = counter.QuadPart % freq.QuadPart;
auto dur = seconds * nano::den + fractions * nano::den / freq.QuadPart;
return steady_clock::time_point(steady_clock::duration(dur));
}
#elif defined(CLOCK_MONOTONIC)
static steady_clock::time_point __libcpp_steady_clock_now() {
struct timespec tp;
if (0 != clock_gettime(CLOCK_MONOTONIC, &tp))
__throw_system_error(errno, "clock_gettime(CLOCK_MONOTONIC) failed");
return steady_clock::time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec));
}
#else
# error "Monotonic clock not implemented on this platform"
#endif
const bool steady_clock::is_steady;
steady_clock::time_point
steady_clock::now() _NOEXCEPT
{
return __libcpp_steady_clock_now();
}
#endif
}
_LIBCPP_END_NAMESPACE_STD