Fix up and namespace clock_gettime and clock_getres for MacOS
[folly.git] / folly / portability / Time.cpp
old mode 100755 (executable)
new mode 100644 (file)
index 51a8b87..e54286f
@@ -32,7 +32,7 @@ static void duration_to_ts(
                          .count());
 }
 
-#if !FOLLY_HAVE_CLOCK_GETTIME
+#if !FOLLY_HAVE_CLOCK_GETTIME || FOLLY_FORCE_CLOCK_GETTIME_DEFINITION
 #if __MACH__
 #include <errno.h>
 #include <mach/mach_init.h>
@@ -43,6 +43,10 @@ static void duration_to_ts(
 #include <mach/thread_act.h>
 #include <mach/vm_map.h>
 
+namespace folly {
+namespace portability {
+namespace time {
+
 static std::chrono::nanoseconds time_value_to_ns(time_value_t t) {
   return std::chrono::seconds(t.seconds) +
       std::chrono::microseconds(t.microseconds);
@@ -136,6 +140,10 @@ int clock_getres(clockid_t clk_id, struct timespec* ts) {
 
   return 0;
 }
+
+} // namespace time
+} // namespace portability
+} // namespace folly
 #elif defined(_WIN32)
 #include <errno.h>
 #include <locale.h>
@@ -144,6 +152,10 @@ int clock_getres(clockid_t clk_id, struct timespec* ts) {
 
 #include <folly/portability/Windows.h>
 
+namespace folly {
+namespace portability {
+namespace time {
+
 using unsigned_nanos = std::chrono::duration<uint64_t, std::nano>;
 
 static unsigned_nanos filetimeToUnsignedNanos(FILETIME ft) {
@@ -198,7 +210,7 @@ extern "C" int clock_getres(clockid_t clock_id, struct timespec* res) {
       }
 
       res->tv_sec = 0;
-      res->tv_nsec = timeIncrement * 100;
+      res->tv_nsec = long(timeIncrement * 100);
       return 0;
     }
 
@@ -215,7 +227,7 @@ extern "C" int clock_gettime(clockid_t clock_id, struct timespec* tp) {
   }
 
   const auto unanosToTimespec = [](timespec* tp, unsigned_nanos t) -> int {
-    static constexpr unsigned_nanos one_sec(std::chrono::seconds(1));
+    static constexpr unsigned_nanos one_sec{std::chrono::seconds(1)};
     tp->tv_sec =
         time_t(std::chrono::duration_cast<std::chrono::seconds>(t).count());
     tp->tv_nsec = long((t % one_sec).count());
@@ -272,6 +284,10 @@ extern "C" int clock_gettime(clockid_t clock_id, struct timespec* tp) {
       return -1;
   }
 }
+
+} // namespace time
+} // namespace portability
+} // namespace folly
 #else
 #error No clock_gettime(3) compatibility wrapper available for this platform.
 #endif
@@ -325,9 +341,10 @@ int nanosleep(const struct timespec* request, struct timespec* remain) {
   return 0;
 }
 
-char* strptime(const char* __restrict s,
-               const char* __restrict f,
-               struct tm* __restrict tm) {
+char* strptime(
+    const char* __restrict s,
+    const char* __restrict f,
+    struct tm* __restrict tm) {
   // Isn't the C++ standard lib nice? std::get_time is defined such that its
   // format parameters are the exact same as strptime. Of course, we have to
   // create a string stream first, and imbue it with the current C locale, and