Skip to content

Commit fb9575e

Browse files
MedourMehdimikrosk
authored andcommitted
Minimal implementation of clock_gettime function
1 parent 059189c commit fb9575e

File tree

4 files changed

+43
-0
lines changed

4 files changed

+43
-0
lines changed

include/bits/time.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
# ifdef __USE_POSIX199309
3232
/* Identifier for system-wide realtime clock. */
3333
# define CLOCK_REALTIME 0
34+
# define CLOCK_MONOTONIC 1
3435

3536
/* Flag to indicate time is absolute. */
3637
# define TIMER_ABSTIME 1

include/time.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,8 @@ extern int dysize (int __year) __THROW __attribute__ ((__const__));
277277
__THROW. */
278278
extern int nanosleep (__const struct timespec *__requested_time,
279279
struct timespec *__remaining);
280+
281+
extern int clock_gettime(clockid_t clock_id, struct timespec *tp);
280282
# endif
281283

282284
# ifdef __USE_XOPEN_EXTENDED

posix/SRCFILES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ SRCFILES = \
2020
glob.c \
2121
isfdtype.c \
2222
nanosleep.c \
23+
clock_gettime.c \
2324
posix_fallocate.c \
2425
pread.c \
2526
pwrite.c \

posix/clock_gettime.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include <errno.h>
2+
#include <stdint.h>
3+
#include <time.h>
4+
#include <sys/time.h>
5+
6+
static inline int
7+
realtime_gettime(struct timespec *tp)
8+
{
9+
struct timeval tv;
10+
int retval = __gettimeofday(&tv, NULL);
11+
if (retval == 0)
12+
/* Convert into `timespec'. */
13+
TIMEVAL_TO_TIMESPEC(&tv, tp);
14+
return retval;
15+
}
16+
17+
/* Get current value of CLOCK and store it in TP. */
18+
19+
__typeof__(clock_gettime) __clock_gettime;
20+
21+
int
22+
__clock_gettime(clockid_t clock_id, struct timespec *tp)
23+
{
24+
int retval = -1;
25+
26+
switch (clock_id)
27+
{
28+
case CLOCK_MONOTONIC:
29+
case CLOCK_REALTIME:
30+
retval = realtime_gettime(tp);
31+
break;
32+
default:
33+
__set_errno(EINVAL);
34+
break;
35+
}
36+
return retval;
37+
}
38+
39+
weak_alias (__clock_gettime, clock_gettime)

0 commit comments

Comments
 (0)