xref: /aosp_15_r20/external/llvm-libc/src/time/linux/time.cpp (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
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 //===----------------------------------------------------------------------===//
8 
9 #include "hdr/time_macros.h"
10 #include "src/__support/common.h"
11 #include "src/__support/macros/config.h"
12 #include "src/__support/time/linux/clock_gettime.h"
13 #include "src/errno/libc_errno.h"
14 #include "src/time/time_func.h"
15 
16 namespace LIBC_NAMESPACE_DECL {
17 
18 LLVM_LIBC_FUNCTION(time_t, time, (time_t * tp)) {
19   // TODO: Use the Linux VDSO to fetch the time and avoid the syscall.
20   struct timespec ts;
21   auto result = internal::clock_gettime(CLOCK_REALTIME, &ts);
22   if (!result.has_value()) {
23     libc_errno = result.error();
24     return -1;
25   }
26 
27   if (tp != nullptr)
28     *tp = time_t(ts.tv_sec);
29   return time_t(ts.tv_sec);
30 }
31 
32 } // namespace LIBC_NAMESPACE_DECL
33