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 //===----------------------------------------------------------------------===// 8 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" 15 16 namespace LIBC_NAMESPACE_DECL { 17 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; 35 } 36 37 auto result = internal::clock_gettime(clockid, ts); 38 if (!result.has_value()) { 39 libc_errno = result.error(); 40 return 0; 41 } 42 return base; 43 } 44 45 } // namespace LIBC_NAMESPACE_DECL 46