Path: blob/main/contrib/llvm-project/libcxx/src/filesystem/filesystem_clock.cpp
35231 views
//===----------------------------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include <__config>9#include <chrono>10#include <filesystem>11#include <time.h>1213#if defined(_LIBCPP_WIN32API)14# include "time_utils.h"15#endif1617#if defined(_LIBCPP_WIN32API)18# define WIN32_LEAN_AND_MEAN19# define NOMINMAX20# include <windows.h>21#endif2223#if __has_include(<unistd.h>)24# include <unistd.h> // _POSIX_TIMERS25#endif2627#if __has_include(<sys/time.h>)28# include <sys/time.h> // for gettimeofday and timeval29#endif3031#if defined(__APPLE__) || defined(__gnu_hurd__) || (defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0)32# define _LIBCPP_HAS_CLOCK_GETTIME33#endif3435_LIBCPP_BEGIN_NAMESPACE_FILESYSTEM3637const bool _FilesystemClock::is_steady;3839_FilesystemClock::time_point _FilesystemClock::now() noexcept {40typedef chrono::duration<rep> __secs;41#if defined(_LIBCPP_WIN32API)42typedef chrono::duration<rep, nano> __nsecs;43FILETIME time;44GetSystemTimeAsFileTime(&time);45detail::TimeSpec tp = detail::filetime_to_timespec(time);46return time_point(__secs(tp.tv_sec) + chrono::duration_cast<duration>(__nsecs(tp.tv_nsec)));47#elif defined(_LIBCPP_HAS_CLOCK_GETTIME)48typedef chrono::duration<rep, nano> __nsecs;49struct timespec tp;50if (0 != clock_gettime(CLOCK_REALTIME, &tp))51__throw_system_error(errno, "clock_gettime(CLOCK_REALTIME) failed");52return time_point(__secs(tp.tv_sec) + chrono::duration_cast<duration>(__nsecs(tp.tv_nsec)));53#else54typedef chrono::duration<rep, micro> __microsecs;55timeval tv;56gettimeofday(&tv, 0);57return time_point(__secs(tv.tv_sec) + __microsecs(tv.tv_usec));58#endif59}6061_LIBCPP_END_NAMESPACE_FILESYSTEM626364