[TargetLowering] Return Align from getByValTypeAlignment (NFC) (#119233)
[llvm-project.git] / libc / src / time / linux / gettimeofday.cpp
blobe8ddf482fc98402800378f3385b0301b4f693515
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"
10 #include "hdr/time_macros.h"
11 #include "hdr/types/suseconds_t.h"
12 #include "src/__support/common.h"
13 #include "src/__support/macros/config.h"
14 #include "src/__support/time/clock_gettime.h"
15 #include "src/__support/time/units.h"
16 #include "src/errno/libc_errno.h"
18 namespace LIBC_NAMESPACE_DECL {
20 // TODO(michaelrj): Move this into time/linux with the other syscalls.
21 LLVM_LIBC_FUNCTION(int, gettimeofday,
22 (struct timeval * tv, [[maybe_unused]] void *unused)) {
23 using namespace time_units;
24 if (tv == nullptr)
25 return 0;
27 struct timespec ts;
28 auto result = internal::clock_gettime(CLOCK_REALTIME, &ts);
30 // A negative return value indicates an error with the magnitude of the
31 // value being the error code.
32 if (!result.has_value()) {
33 libc_errno = result.error();
34 return -1;
37 tv->tv_sec = ts.tv_sec;
38 tv->tv_usec = static_cast<suseconds_t>(ts.tv_nsec / 1_us_ns);
39 return 0;
42 } // namespace LIBC_NAMESPACE_DECL