1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
10 #include <__system_error/throw_system_error.h>
15 #if defined(_LIBCPP_WIN32API)
16 # include "time_utils.h"
19 #if defined(_LIBCPP_WIN32API)
20 # define WIN32_LEAN_AND_MEAN
25 #if __has_include(<unistd.h>)
26 # include <unistd.h> // _POSIX_TIMERS
29 #if __has_include(<sys/time.h>)
30 # include <sys/time.h> // for gettimeofday and timeval
33 #if defined(__LLVM_LIBC__)
34 # define _LIBCPP_HAS_TIMESPEC_GET
37 #if defined(__APPLE__) || defined(__gnu_hurd__) || defined(__AMDGPU__) || defined(__NVPTX__) || \
38 (defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0)
39 # define _LIBCPP_HAS_CLOCK_GETTIME
42 _LIBCPP_BEGIN_NAMESPACE_FILESYSTEM
44 _LIBCPP_DIAGNOSTIC_PUSH
45 _LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wdeprecated")
46 const bool _FilesystemClock::is_steady
;
47 _LIBCPP_DIAGNOSTIC_POP
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
;
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
;
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
;
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
)));
70 typedef chrono::duration
<rep
, micro
> __microsecs
;
73 return time_point(__secs(tv
.tv_sec
) + __microsecs(tv
.tv_usec
));
77 _LIBCPP_END_NAMESPACE_FILESYSTEM