Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / src / filesystem / filesystem_clock.cpp
blobfbb19ac68df55fd39cda34141a5f9e4c1570318d
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 //===----------------------------------------------------------------------===//
9 #include <__config>
10 #include <chrono>
11 #include <filesystem>
12 #include <time.h>
14 #if defined(_LIBCPP_WIN32API)
15 # include "time_utils.h"
16 #endif
18 #if defined(_LIBCPP_WIN32API)
19 # define WIN32_LEAN_AND_MEAN
20 # define NOMINMAX
21 # include <windows.h>
22 #endif
24 #if __has_include(<unistd.h>)
25 # include <unistd.h> // _POSIX_TIMERS
26 #endif
28 #if __has_include(<sys/time.h>)
29 # include <sys/time.h> // for gettimeofday and timeval
30 #endif
32 #if defined(__APPLE__) || defined (__gnu_hurd__) || (defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0)
33 # define _LIBCPP_HAS_CLOCK_GETTIME
34 #endif
36 _LIBCPP_BEGIN_NAMESPACE_FILESYSTEM
38 const bool _FilesystemClock::is_steady;
40 _FilesystemClock::time_point _FilesystemClock::now() noexcept {
41 typedef chrono::duration<rep> __secs;
42 #if defined(_LIBCPP_WIN32API)
43 typedef chrono::duration<rep, nano> __nsecs;
44 FILETIME time;
45 GetSystemTimeAsFileTime(&time);
46 detail::TimeSpec tp = detail::filetime_to_timespec(time);
47 return time_point(__secs(tp.tv_sec) +
48 chrono::duration_cast<duration>(__nsecs(tp.tv_nsec)));
49 #elif defined(_LIBCPP_HAS_CLOCK_GETTIME)
50 typedef chrono::duration<rep, nano> __nsecs;
51 struct timespec tp;
52 if (0 != clock_gettime(CLOCK_REALTIME, &tp))
53 __throw_system_error(errno, "clock_gettime(CLOCK_REALTIME) failed");
54 return time_point(__secs(tp.tv_sec) +
55 chrono::duration_cast<duration>(__nsecs(tp.tv_nsec)));
56 #else
57 typedef chrono::duration<rep, micro> __microsecs;
58 timeval tv;
59 gettimeofday(&tv, 0);
60 return time_point(__secs(tv.tv_sec) + __microsecs(tv.tv_usec));
61 #endif
64 _LIBCPP_END_NAMESPACE_FILESYSTEM