[libc][NFC] Move aligned access implementations to separate header
[llvm-project.git] / libc / src / time / linux / time.cpp
blob02b55a2f4c02678a3a972ceccc11ffab3ababca9
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"
15 #include <sys/syscall.h> // For syscall numbers.
16 #include <time.h>
18 namespace __llvm_libc {
20 LLVM_LIBC_FUNCTION(time_t, time, (time_t * tp)) {
21 // TODO: Use the Linux VDSO to fetch the time and avoid the syscall.
22 struct timespec ts;
23 long ret_val = __llvm_libc::syscall_impl(SYS_clock_gettime, CLOCK_REALTIME,
24 reinterpret_cast<long>(&ts));
25 if (ret_val < 0) {
26 libc_errno = -ret_val;
27 return -1;
30 if (tp != nullptr)
31 *tp = time_t(ts.tv_sec);
32 return time_t(ts.tv_sec);
35 } // namespace __llvm_libc