m68k: enable time64
[uclibc-ng.git] / librt / clock_gettime.c
blobeab9a3343621154124701494e312045fa1c32ced
1 /* clock_gettime -- Get current time from a POSIX clockid_t. Linux version.
2 Copyright (C) 2003, 2004 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #include <errno.h>
20 #include <sysdep.h>
21 #include <time.h>
22 #include <sys/time.h>
23 #include "kernel-posix-cpu-timers.h"
25 #if defined(__UCLIBC_USE_TIME64__)
26 #include "internal/time64_helpers.h"
27 #endif
29 #if defined(__UCLIBC_USE_TIME64__) && defined(__NR_clock_gettime64)
30 #define SYSCALL_GETTIME \
31 { \
32 struct __ts64_struct __ts64; \
33 retval = INLINE_SYSCALL (clock_gettime64, 2, clock_id, &__ts64); \
34 if (tp) { \
35 tp->tv_sec = __ts64.tv_sec; \
36 tp->tv_nsec = __ts64.tv_nsec; \
37 } \
38 break; \
40 #else
41 #define SYSCALL_GETTIME \
42 retval = INLINE_SYSCALL (clock_gettime, 2, clock_id, tp); \
43 break
44 #endif
46 /* The REALTIME and MONOTONIC clock are definitely supported in the kernel. */
47 #define SYSDEP_GETTIME \
48 SYSDEP_GETTIME_CPUTIME \
49 case CLOCK_REALTIME: \
50 case CLOCK_MONOTONIC: \
51 SYSCALL_GETTIME
53 /* We handled the REALTIME clock here. */
54 #define HANDLED_REALTIME 1
55 #define HANDLED_CPUTIME 1
57 #define SYSDEP_GETTIME_CPU SYSCALL_GETTIME
58 #define SYSDEP_GETTIME_CPUTIME /* Default catches them too. */
60 static inline int
61 realtime_gettime (struct timespec *tp)
63 struct timeval tv;
64 int retval = gettimeofday (&tv, NULL);
65 if (retval == 0)
66 /* Convert into `timespec'. */
67 TIMEVAL_TO_TIMESPEC (&tv, tp);
68 return retval;
71 /* Get current value of CLOCK and store it in TP. */
72 int
73 clock_gettime (clockid_t clock_id, struct timespec *tp)
75 int retval = -1;
76 #ifndef HANDLED_REALTIME
77 struct timeval tv;
78 #endif
80 switch (clock_id)
82 #ifdef SYSDEP_GETTIME
83 SYSDEP_GETTIME;
84 #endif
86 #ifndef HANDLED_REALTIME
87 case CLOCK_REALTIME:
88 retval = gettimeofday (&tv, NULL);
89 if (retval == 0)
90 TIMEVAL_TO_TIMESPEC (&tv, tp);
91 break;
92 #endif
94 default:
95 #ifdef SYSDEP_GETTIME_CPU
96 SYSDEP_GETTIME_CPU;
97 #endif
98 __set_errno (EINVAL);
99 break;
102 return retval;