[flang] Accept polymorphic component element in storage_size
[llvm-project.git] / libc / src / time / gettimeofday.cpp
blob266ba14648a2c07265bde669ac4d78c914fb7804
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"
14 #include <errno.h>
15 #include <sys/syscall.h> // For syscall numbers.
17 namespace __llvm_libc {
19 LLVM_LIBC_FUNCTION(int, gettimeofday,
20 (struct timeval * tv, [[maybe_unused]] void *unused)) {
21 if (tv == nullptr)
22 return 0;
23 struct timespec tp;
24 long ret_val = __llvm_libc::syscall_impl(SYS_clock_gettime,
25 static_cast<long>(CLOCK_REALTIME),
26 reinterpret_cast<long>(&tp));
27 // A negative return value indicates an error with the magnitude of the
28 // value being the error code.
29 if (ret_val < 0) {
30 errno = -ret_val;
31 return -1;
33 tv->tv_sec = tp.tv_sec;
34 tv->tv_usec = tp.tv_nsec / 1000;
35 return 0;
38 } // namespace __llvm_libc