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-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include <__config> 10 #include <__system_error/throw_system_error.h> 11 #include <chrono> 12 #include <filesystem> 13 #include <time.h> 14 15 #if defined(_LIBCPP_WIN32API) 16 # include "time_utils.h" 17 #endif 18 19 #if defined(_LIBCPP_WIN32API) 20 # define WIN32_LEAN_AND_MEAN 21 # define NOMINMAX 22 # include <windows.h> 23 #endif 24 25 #if __has_include(<unistd.h>) 26 # include <unistd.h> // _POSIX_TIMERS 27 #endif 28 29 #if __has_include(<sys/time.h>) 30 # include <sys/time.h> // for gettimeofday and timeval 31 #endif 32 33 #if defined(__LLVM_LIBC__) 34 # define _LIBCPP_HAS_TIMESPEC_GET 35 #endif 36 37 #if defined(__APPLE__) || defined(__gnu_hurd__) || defined(__AMDGPU__) || defined(__NVPTX__) || \ 38 (defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0) 39 # define _LIBCPP_HAS_CLOCK_GETTIME 40 #endif 41 42 _LIBCPP_BEGIN_NAMESPACE_FILESYSTEM 43 44 _LIBCPP_DIAGNOSTIC_PUSH 45 _LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wdeprecated") 46 const bool _FilesystemClock::is_steady; 47 _LIBCPP_DIAGNOSTIC_POP 48 49 _FilesystemClock::time_point _FilesystemClock::now() noexcept { 50 typedef chrono::duration<rep> __secs; 51 #if defined(_LIBCPP_WIN32API) 52 typedef chrono::duration<rep, nano> __nsecs; 53 FILETIME time; 54 GetSystemTimeAsFileTime(&time); 55 detail::TimeSpec tp = detail::filetime_to_timespec(time); 56 return time_point(__secs(tp.tv_sec) + chrono::duration_cast<duration>(__nsecs(tp.tv_nsec))); 57 #elif defined(_LIBCPP_HAS_TIMESPEC_GET) 58 typedef chrono::duration<rep, nano> __nsecs; 59 struct timespec ts; 60 if (timespec_get(&ts, TIME_UTC) != TIME_UTC) 61 __throw_system_error(errno, "timespec_get(TIME_UTC) failed"); 62 return time_point(__secs(ts.tv_sec) + chrono::duration_cast<duration>(__nsecs(ts.tv_nsec))); 63 #elif defined(_LIBCPP_HAS_CLOCK_GETTIME) 64 typedef chrono::duration<rep, nano> __nsecs; 65 struct timespec tp; 66 if (0 != clock_gettime(CLOCK_REALTIME, &tp)) 67 __throw_system_error(errno, "clock_gettime(CLOCK_REALTIME) failed"); 68 return time_point(__secs(tp.tv_sec) + chrono::duration_cast<duration>(__nsecs(tp.tv_nsec))); 69 #else 70 typedef chrono::duration<rep, micro> __microsecs; 71 timeval tv; 72 gettimeofday(&tv, 0); 73 return time_point(__secs(tv.tv_sec) + __microsecs(tv.tv_usec)); 74 #endif 75 } 76 77 _LIBCPP_END_NAMESPACE_FILESYSTEM 78