get_current_time_posix.inc 589 B

123456789101112131415161718192021222324
  1. #include "absl/time/clock.h"
  2. #include <sys/time.h>
  3. #include <ctime>
  4. #include <cstdint>
  5. #include "absl/base/internal/raw_logging.h"
  6. namespace absl {
  7. ABSL_NAMESPACE_BEGIN
  8. namespace time_internal {
  9. static int64_t GetCurrentTimeNanosFromSystem() {
  10. const int64_t kNanosPerSecond = 1000 * 1000 * 1000;
  11. struct timespec ts;
  12. ABSL_RAW_CHECK(clock_gettime(CLOCK_REALTIME, &ts) == 0,
  13. "Failed to read real-time clock.");
  14. return (int64_t{ts.tv_sec} * kNanosPerSecond +
  15. int64_t{ts.tv_nsec});
  16. }
  17. } // namespace time_internal
  18. ABSL_NAMESPACE_END
  19. } // namespace absl