Skip to content

Commit

Permalink
Minimal implementation of clock_gettime function
Browse files Browse the repository at this point in the history
  • Loading branch information
MedourMehdi authored and mikrosk committed Oct 5, 2024
1 parent 059189c commit fb9575e
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/bits/time.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
# ifdef __USE_POSIX199309
/* Identifier for system-wide realtime clock. */
# define CLOCK_REALTIME 0
# define CLOCK_MONOTONIC 1

/* Flag to indicate time is absolute. */
# define TIMER_ABSTIME 1
Expand Down
2 changes: 2 additions & 0 deletions include/time.h
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,8 @@ extern int dysize (int __year) __THROW __attribute__ ((__const__));
__THROW. */
extern int nanosleep (__const struct timespec *__requested_time,
struct timespec *__remaining);

extern int clock_gettime(clockid_t clock_id, struct timespec *tp);
# endif

# ifdef __USE_XOPEN_EXTENDED
Expand Down
1 change: 1 addition & 0 deletions posix/SRCFILES
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ SRCFILES = \
glob.c \
isfdtype.c \
nanosleep.c \
clock_gettime.c \
posix_fallocate.c \
pread.c \
pwrite.c \
Expand Down
39 changes: 39 additions & 0 deletions posix/clock_gettime.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <errno.h>
#include <stdint.h>
#include <time.h>
#include <sys/time.h>

static inline int
realtime_gettime(struct timespec *tp)
{
struct timeval tv;
int retval = __gettimeofday(&tv, NULL);
if (retval == 0)
/* Convert into `timespec'. */
TIMEVAL_TO_TIMESPEC(&tv, tp);
return retval;
}

/* Get current value of CLOCK and store it in TP. */

__typeof__(clock_gettime) __clock_gettime;

int
__clock_gettime(clockid_t clock_id, struct timespec *tp)
{
int retval = -1;

switch (clock_id)
{
case CLOCK_MONOTONIC:
case CLOCK_REALTIME:
retval = realtime_gettime(tp);
break;
default:
__set_errno(EINVAL);
break;
}
return retval;
}

weak_alias (__clock_gettime, clock_gettime)

0 comments on commit fb9575e

Please sign in to comment.