get_current_time_ios.inc 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. // These are not defined in the Xcode 7.3.1 SDK Headers.
  7. // Once we are no longer supporting Xcode 7.3.1 we can
  8. // remove these.
  9. #ifndef __WATCHOS_3_0
  10. #define __WATCHOS_3_0 30000
  11. #endif
  12. #ifndef __TVOS_10_0
  13. #define __TVOS_10_0 100000
  14. #endif
  15. #ifndef __IPHONE_10_0
  16. #define __IPHONE_10_0 100000
  17. #endif
  18. #ifndef __MAC_10_12
  19. #define __MAC_10_12 101200
  20. #endif
  21. namespace absl {
  22. inline namespace lts_2018_06_20 {
  23. namespace time_internal {
  24. static int64_t GetCurrentTimeNanosFromSystem() {
  25. #if (__MAC_OS_X_VERSION_MAX_ALLOWED >= __MAC_10_12) || \
  26. (__IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0) || \
  27. (__WATCH_OS_VERSION_MAX_ALLOWED >= __WATCHOS_3_0) || \
  28. (__TV_OS_VERSION_MAX_ALLOWED >= __TVOS_10_0)
  29. // clock_gettime_nsec_np is not defined on SDKs before Xcode 8.0.
  30. // This preprocessor logic is based upon __CLOCK_AVAILABILITY in
  31. // usr/include/time.h. Once we are no longer supporting Xcode 7.3.1 we can
  32. // remove this #if.
  33. // We must continue to check if it is defined until we are sure that ALL the
  34. // platforms we are shipping on support it.
  35. // clock_gettime_nsec_np is preferred because it may give higher accuracy than
  36. // gettimeofday in future Apple operating systems.
  37. // Currently (macOS 10.12/iOS 10.2) clock_gettime_nsec_np accuracy is
  38. // microsecond accuracy (i.e. equivalent to gettimeofday).
  39. if (&clock_gettime_nsec_np != nullptr) {
  40. return clock_gettime_nsec_np(CLOCK_REALTIME);
  41. }
  42. #endif
  43. #if (defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && \
  44. (__MAC_OS_X_VERSION_MIN_REQUIRED < __MAC_10_12)) || \
  45. (defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && \
  46. (__IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_10_0)) || \
  47. (defined(__WATCH_OS_VERSION_MIN_REQUIRED) && \
  48. (__WATCH_OS_VERSION_MIN_REQUIRED < __WATCHOS_3_0)) || \
  49. (defined(__TV_OS_VERSION_MIN_REQUIRED) && \
  50. (__TV_OS_VERSION_MIN_REQUIRED < __TVOS_10_0))
  51. // We need this block in 2 different cases:
  52. // a) where we are compiling with Xcode 7 in which case the block above
  53. // will not be compiled in, and this is the only block executed.
  54. // b) where we are compiling with Xcode 8+ but supporting operating systems
  55. // that do not define clock_gettime_nsec_np, so this is in effect
  56. // an else block to the block above.
  57. // This block will not be compiled if the min supported version is
  58. // guaranteed to supply clock_gettime_nsec_np.
  59. //
  60. // Once we know that clock_gettime_nsec_np is in the SDK *AND* exists on
  61. // all the platforms we support, we can remove both this block and alter the
  62. // block above to just call clock_gettime_nsec_np directly.
  63. const int64_t kNanosPerSecond = 1000 * 1000 * 1000;
  64. const int64_t kNanosPerMicrosecond = 1000;
  65. struct timeval tp;
  66. ABSL_RAW_CHECK(gettimeofday(&tp, nullptr) == 0, "Failed gettimeofday");
  67. return (int64_t{tp.tv_sec} * kNanosPerSecond +
  68. int64_t{tp.tv_usec} * kNanosPerMicrosecond);
  69. #endif
  70. }
  71. } // namespace time_internal
  72. } // inline namespace lts_2018_06_20
  73. } // namespace absl