1 //===-- Implementation of gettimeofday function ---------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "src/time/gettimeofday.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.
17 namespace __llvm_libc
{
19 // TODO(michaelrj): Move this into time/linux with the other syscalls.
20 LLVM_LIBC_FUNCTION(int, gettimeofday
,
21 (struct timeval
* tv
, [[maybe_unused
]] void *unused
)) {
25 long ret_val
= __llvm_libc::syscall_impl(SYS_clock_gettime
,
26 static_cast<long>(CLOCK_REALTIME
),
27 reinterpret_cast<long>(&tp
));
28 // A negative return value indicates an error with the magnitude of the
29 // value being the error code.
31 libc_errno
= -ret_val
;
34 tv
->tv_sec
= tp
.tv_sec
;
35 tv
->tv_usec
= tp
.tv_nsec
/ 1000;
39 } // namespace __llvm_libc