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 "src/time/time_func.h"
10
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"
15
16#include <sys/syscall.h> // For syscall numbers.
17#include <time.h>
18
19namespace LIBC_NAMESPACE {
20
21LLVM_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: &ts);
25 if (!result.has_value()) {
26 libc_errno = result.error();
27 return -1;
28 }
29
30 if (tp != nullptr)
31 *tp = time_t(ts.tv_sec);
32 return time_t(ts.tv_sec);
33}
34
35} // namespace LIBC_NAMESPACE
36

source code of libc/src/time/linux/time.cpp