80 lines · cpp
1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#include <__config>10#include <__system_error/throw_system_error.h>11#include <cerrno>12#include <chrono>13#include <filesystem>14#include <ratio>15#include <time.h>16 17#if defined(_LIBCPP_WIN32API)18# include "time_utils.h"19#endif20 21#if defined(_LIBCPP_WIN32API)22# define WIN32_LEAN_AND_MEAN23# define NOMINMAX24# include <windows.h>25#endif26 27#if __has_include(<unistd.h>)28# include <unistd.h> // _POSIX_TIMERS29#endif30 31#if __has_include(<sys/time.h>)32# include <sys/time.h> // for gettimeofday and timeval33#endif34 35#if defined(__LLVM_LIBC__)36# define _LIBCPP_HAS_TIMESPEC_GET37#endif38 39#if defined(__APPLE__) || defined(__gnu_hurd__) || defined(__AMDGPU__) || defined(__NVPTX__) || \40 (defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0)41# define _LIBCPP_HAS_CLOCK_GETTIME42#endif43 44_LIBCPP_BEGIN_NAMESPACE_FILESYSTEM45 46_LIBCPP_DIAGNOSTIC_PUSH47_LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wdeprecated")48const bool _FilesystemClock::is_steady;49_LIBCPP_DIAGNOSTIC_POP50 51_FilesystemClock::time_point _FilesystemClock::now() noexcept {52 typedef chrono::duration<rep> __secs;53#if defined(_LIBCPP_WIN32API)54 typedef chrono::duration<rep, nano> __nsecs;55 FILETIME time;56 GetSystemTimeAsFileTime(&time);57 detail::TimeSpec tp = detail::filetime_to_timespec(time);58 return time_point(__secs(tp.tv_sec) + chrono::duration_cast<duration>(__nsecs(tp.tv_nsec)));59#elif defined(_LIBCPP_HAS_TIMESPEC_GET)60 typedef chrono::duration<rep, nano> __nsecs;61 struct timespec ts;62 if (timespec_get(&ts, TIME_UTC) != TIME_UTC)63 std::__throw_system_error(errno, "timespec_get(TIME_UTC) failed");64 return time_point(__secs(ts.tv_sec) + chrono::duration_cast<duration>(__nsecs(ts.tv_nsec)));65#elif defined(_LIBCPP_HAS_CLOCK_GETTIME)66 typedef chrono::duration<rep, nano> __nsecs;67 struct timespec tp;68 if (0 != clock_gettime(CLOCK_REALTIME, &tp))69 std::__throw_system_error(errno, "clock_gettime(CLOCK_REALTIME) failed");70 return time_point(__secs(tp.tv_sec) + chrono::duration_cast<duration>(__nsecs(tp.tv_nsec)));71#else72 typedef chrono::duration<rep, micro> __microsecs;73 timeval tv;74 gettimeofday(&tv, 0);75 return time_point(__secs(tv.tv_sec) + __microsecs(tv.tv_usec));76#endif77}78 79_LIBCPP_END_NAMESPACE_FILESYSTEM80