[libc][NFC] Move aligned access implementations to separate header
[llvm-project.git] / libc / src / time / gettimeofday.cpp
blob914b5826e9cfb8a33e8420546cffc28faf191411
1 //===-- Implementation of gettimeofday 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/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)) {
22 if (tv == nullptr)
23 return 0;
24 struct timespec tp;
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.
30 if (ret_val < 0) {
31 libc_errno = -ret_val;
32 return -1;
34 tv->tv_sec = tp.tv_sec;
35 tv->tv_usec = tp.tv_nsec / 1000;
36 return 0;
39 } // namespace __llvm_libc