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 <chrono> 11 #include <filesystem> 12 #include <time.h> 13 14 #include "time_utils.h" 15 16 #if defined(_LIBCPP_WIN32API) 17 # define WIN32_LEAN_AND_MEAN 18 # define NOMINMAX 19 # include <windows.h> 20 #endif 21 22 #if __has_include(<unistd.h>) 23 # include <unistd.h> // _POSIX_TIMERS 24 #endif 25 26 #if __has_include(<sys/time.h>) 27 # include <sys/time.h> // for gettimeofday and timeval 28 #endif 29 30 #if !defined(__APPLE__) && defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0 31 # define _LIBCPP_USE_CLOCK_GETTIME 32 #endif 33 34 _LIBCPP_BEGIN_NAMESPACE_FILESYSTEM 35 36 const bool _FilesystemClock::is_steady; 37 38 _FilesystemClock::time_point _FilesystemClock::now() noexcept { 39 typedef chrono::duration<rep> __secs; 40 #if defined(_LIBCPP_WIN32API) 41 typedef chrono::duration<rep, nano> __nsecs; 42 FILETIME time; 43 GetSystemTimeAsFileTime(&time); 44 detail::TimeSpec tp = detail::filetime_to_timespec(time); 45 return time_point(__secs(tp.tv_sec) + 46 chrono::duration_cast<duration>(__nsecs(tp.tv_nsec))); 47 #elif defined(_LIBCPP_USE_CLOCK_GETTIME) && defined(CLOCK_REALTIME) 48 typedef chrono::duration<rep, nano> __nsecs; 49 struct timespec tp; 50 if (0 != clock_gettime(CLOCK_REALTIME, &tp)) 51 __throw_system_error(errno, "clock_gettime(CLOCK_REALTIME) failed"); 52 return time_point(__secs(tp.tv_sec) + 53 chrono::duration_cast<duration>(__nsecs(tp.tv_nsec))); 54 #else 55 typedef chrono::duration<rep, micro> __microsecs; 56 timeval tv; 57 gettimeofday(&tv, 0); 58 return time_point(__secs(tv.tv_sec) + __microsecs(tv.tv_usec)); 59 #endif // CLOCK_REALTIME 60 } 61 62 _LIBCPP_END_NAMESPACE_FILESYSTEM 63