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 <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 #endif
20
21 #if defined(_LIBCPP_WIN32API)
22 # define WIN32_LEAN_AND_MEAN
23 # define NOMINMAX
24 # include <windows.h>
25 #endif
26
27 #if __has_include(<unistd.h>)
28 # include <unistd.h> // _POSIX_TIMERS
29 #endif
30
31 #if __has_include(<sys/time.h>)
32 # include <sys/time.h> // for gettimeofday and timeval
33 #endif
34
35 #if defined(__LLVM_LIBC__)
36 # define _LIBCPP_HAS_TIMESPEC_GET
37 #endif
38
39 #if defined(__APPLE__) || defined(__gnu_hurd__) || defined(__AMDGPU__) || defined(__NVPTX__) || \
40 (defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0)
41 # define _LIBCPP_HAS_CLOCK_GETTIME
42 #endif
43
44 _LIBCPP_BEGIN_NAMESPACE_FILESYSTEM
45
46 _LIBCPP_DIAGNOSTIC_PUSH
47 _LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wdeprecated")
48 const bool _FilesystemClock::is_steady;
49 _LIBCPP_DIAGNOSTIC_POP
50
now()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 #else
72 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 #endif
77 }
78
79 _LIBCPP_END_NAMESPACE_FILESYSTEM
80