[rtsan] NFC: Docs update adding links to Disabling, adding a few ` marks (#119075)
[llvm-project.git] / libc / src / time / linux / timespec_get.cpp
blobcf5174523aa4f95bd25f99ac5e7d80938442d707
1 //===-- Implementation of timespec_get for Linux --------------------------===//
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/timespec_get.h"
10 #include "hdr/time_macros.h"
11 #include "src/__support/common.h"
12 #include "src/__support/macros/config.h"
13 #include "src/__support/time/clock_gettime.h"
14 #include "src/errno/libc_errno.h"
16 namespace LIBC_NAMESPACE_DECL {
18 LLVM_LIBC_FUNCTION(int, timespec_get, (struct timespec * ts, int base)) {
19 clockid_t clockid;
20 switch (base) {
21 case TIME_UTC:
22 clockid = CLOCK_REALTIME;
23 break;
24 case TIME_MONOTONIC:
25 clockid = CLOCK_MONOTONIC;
26 break;
27 case TIME_ACTIVE:
28 clockid = CLOCK_PROCESS_CPUTIME_ID;
29 break;
30 case TIME_THREAD_ACTIVE:
31 clockid = CLOCK_THREAD_CPUTIME_ID;
32 break;
33 default:
34 return 0;
37 auto result = internal::clock_gettime(clockid, ts);
38 if (!result.has_value()) {
39 libc_errno = result.error();
40 return 0;
42 return base;
45 } // namespace LIBC_NAMESPACE_DECL