Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libc / src / time / linux / time.cpp
blobe286fae095b2aeef1606ccb284590dac847111db
1 //===-- Linux implementation of the time function -------------------------===//
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 "src/time/time_func.h"
11 #include "src/__support/OSUtil/syscall.h" // For internal syscall function.
12 #include "src/__support/common.h"
13 #include "src/errno/libc_errno.h"
14 #include "src/time/linux/clockGetTimeImpl.h"
16 #include <sys/syscall.h> // For syscall numbers.
17 #include <time.h>
19 namespace LIBC_NAMESPACE {
21 LLVM_LIBC_FUNCTION(time_t, time, (time_t * tp)) {
22 // TODO: Use the Linux VDSO to fetch the time and avoid the syscall.
23 struct timespec ts;
24 auto result = internal::clock_gettimeimpl(CLOCK_REALTIME, &ts);
25 if (!result.has_value()) {
26 libc_errno = result.error();
27 return -1;
30 if (tp != nullptr)
31 *tp = time_t(ts.tv_sec);
32 return time_t(ts.tv_sec);
35 } // namespace LIBC_NAMESPACE