time.c 974 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Utility routines.
  4. *
  5. * Copyright (C) 2007 Denis Vlasenko
  6. *
  7. * Licensed under GPL version 2, see file LICENSE in this tarball for details.
  8. */
  9. #include "libbb.h"
  10. #if ENABLE_MONOTONIC_SYSCALL
  11. #include <sys/syscall.h>
  12. /* libc has incredibly messy way of doing this,
  13. * typically requiring -lrt. We just skip all this mess */
  14. unsigned long long monotonic_us(void)
  15. {
  16. struct timespec ts;
  17. if (syscall(__NR_clock_gettime, CLOCK_MONOTONIC, &ts))
  18. bb_error_msg_and_die("clock_gettime(MONOTONIC) failed");
  19. return ts.tv_sec * 1000000ULL + ts.tv_nsec/1000;
  20. }
  21. unsigned monotonic_sec(void)
  22. {
  23. struct timespec ts;
  24. if (syscall(__NR_clock_gettime, CLOCK_MONOTONIC, &ts))
  25. bb_error_msg_and_die("clock_gettime(MONOTONIC) failed");
  26. return ts.tv_sec;
  27. }
  28. #else
  29. unsigned long long monotonic_us(void)
  30. {
  31. struct timeval tv;
  32. gettimeofday(&tv, NULL);
  33. return tv.tv_sec * 1000000ULL + tv.tv_usec;
  34. }
  35. unsigned monotonic_sec(void)
  36. {
  37. return time(NULL);
  38. }
  39. #endif