clock_gettime.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. * clock_gettime() for uClibc
  3. *
  4. * Copyright (C) 2003 by Justus Pendleton <uc@ryoohki.net>
  5. * Copyright (C) 2005 by Peter Kjellerstedt <pkj@axis.com>
  6. * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
  7. *
  8. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  9. */
  10. #include <sys/syscall.h>
  11. #include <time.h>
  12. #if defined(__UCLIBC_USE_TIME64__) && defined(__NR_clock_gettime64)
  13. #include "internal/time64_helpers.h"
  14. int clock_gettime(clockid_t clock_id, struct timespec *tp)
  15. {
  16. struct __ts64_struct __ts64;
  17. int __ret = INLINE_SYSCALL(clock_gettime64, 2, clock_id, &__ts64);
  18. if (tp) {
  19. tp->tv_sec = __ts64.tv_sec;
  20. tp->tv_nsec = __ts64.tv_nsec;
  21. }
  22. return __ret;
  23. }
  24. #elif defined(__NR_clock_gettime)
  25. _syscall2(int, clock_gettime, clockid_t, clock_id, struct timespec*, tp)
  26. #else
  27. # include <sys/time.h>
  28. int clock_gettime(clockid_t clock_id, struct timespec* tp)
  29. {
  30. struct timeval tv;
  31. int retval = -1;
  32. switch (clock_id) {
  33. case CLOCK_REALTIME:
  34. /* In Linux, gettimeofday fails only on bad parameter.
  35. * We know that here parameter isn't bad.
  36. */
  37. gettimeofday(&tv, NULL);
  38. TIMEVAL_TO_TIMESPEC(&tv, tp);
  39. retval = 0;
  40. break;
  41. default:
  42. errno = EINVAL;
  43. break;
  44. }
  45. return retval;
  46. }
  47. #endif