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/time/linux/clock_gettime.h" 12 #include "src/errno/libc_errno.h" 13 #include "src/time/time_func.h" 14 15 namespace LIBC_NAMESPACE { 16 17 LLVM_LIBC_FUNCTION(time_t, time, (time_t * tp)) { 18 // TODO: Use the Linux VDSO to fetch the time and avoid the syscall. 19 struct timespec ts; 20 auto result = internal::clock_gettime(CLOCK_REALTIME, &ts); 21 if (!result.has_value()) { 22 libc_errno = result.error(); 23 return -1; 24 } 25 26 if (tp != nullptr) 27 *tp = time_t(ts.tv_sec); 28 return time_t(ts.tv_sec); 29 } 30 31 } // namespace LIBC_NAMESPACE 32