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//===----------------------------------------------------------------------===//
8
9#include "src/time/gettimeofday.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
18namespace LIBC_NAMESPACE {
19
20// TODO(michaelrj): Move this into time/linux with the other syscalls.
21LLVM_LIBC_FUNCTION(int, gettimeofday,
22 (struct timeval * tv, [[maybe_unused]] void *unused)) {
23 if (tv == nullptr)
24 return 0;
25
26 struct timespec ts;
27 auto result = internal::clock_gettimeimpl(CLOCK_REALTIME, ts: &ts);
28
29 // A negative return value indicates an error with the magnitude of the
30 // value being the error code.
31 if (!result.has_value()) {
32 libc_errno = result.error();
33 return -1;
34 }
35
36 tv->tv_sec = ts.tv_sec;
37 tv->tv_usec = static_cast<suseconds_t>(ts.tv_nsec / 1000);
38 return 0;
39}
40
41} // namespace LIBC_NAMESPACE
42

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