Add C11 timespec_get.

Bug: https://github.com/android-ndk/ndk/issues/744
Test: ran tests
Change-Id: Iad9514946e06d55b6a3aa0f945d9a63bff900881
diff --git a/libc/Android.bp b/libc/Android.bp
index 212a1b1..4fc3484 100644
--- a/libc/Android.bp
+++ b/libc/Android.bp
@@ -1389,6 +1389,7 @@
         "bionic/tdestroy.cpp",
         "bionic/termios.cpp",
         "bionic/thread_private.cpp",
+        "bionic/timespec_get.cpp",
         "bionic/tmpfile.cpp",
         "bionic/umount.cpp",
         "bionic/unlink.cpp",
diff --git a/libc/bionic/timespec_get.cpp b/libc/bionic/timespec_get.cpp
new file mode 100644
index 0000000..7fc2182
--- /dev/null
+++ b/libc/bionic/timespec_get.cpp
@@ -0,0 +1,33 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *  * Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *  * Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <time.h>
+
+int timespec_get(timespec* ts, int base) {
+  return (base == TIME_UTC && clock_gettime(CLOCK_REALTIME, ts) != -1) ? base : 0;
+}
diff --git a/libc/include/time.h b/libc/include/time.h
index 17751d7..ea41fda 100644
--- a/libc/include/time.h
+++ b/libc/include/time.h
@@ -108,6 +108,9 @@
 time_t timelocal(struct tm* __tm) __INTRODUCED_IN(12);
 time_t timegm(struct tm* __tm) __INTRODUCED_IN(12);
 
+#define TIME_UTC 1
+int timespec_get(struct timespec* __ts, int __base) __INTRODUCED_IN_FUTURE;
+
 __END_DECLS
 
 #endif
diff --git a/libc/libc.arm.map b/libc/libc.arm.map
index a133804..43c6093 100644
--- a/libc/libc.arm.map
+++ b/libc/libc.arm.map
@@ -1423,6 +1423,7 @@
 LIBC_Q { # introduced=Q
   global:
     __res_randomid;
+    timespec_get;
 } LIBC_P;
 
 LIBC_PRIVATE {
diff --git a/libc/libc.arm64.map b/libc/libc.arm64.map
index b7f7c89..3e20d7e 100644
--- a/libc/libc.arm64.map
+++ b/libc/libc.arm64.map
@@ -1344,6 +1344,7 @@
 LIBC_Q { # introduced=Q
   global:
     __res_randomid;
+    timespec_get;
 } LIBC_P;
 
 LIBC_PRIVATE {
diff --git a/libc/libc.map.txt b/libc/libc.map.txt
index 16fc674..d6b35fd 100644
--- a/libc/libc.map.txt
+++ b/libc/libc.map.txt
@@ -1448,6 +1448,7 @@
 LIBC_Q { # introduced=Q
   global:
     __res_randomid;
+    timespec_get;
 } LIBC_P;
 
 LIBC_PRIVATE {
diff --git a/libc/libc.mips.map b/libc/libc.mips.map
index fa858f0..d163991 100644
--- a/libc/libc.mips.map
+++ b/libc/libc.mips.map
@@ -1407,6 +1407,7 @@
 LIBC_Q { # introduced=Q
   global:
     __res_randomid;
+    timespec_get;
 } LIBC_P;
 
 LIBC_PRIVATE {
diff --git a/libc/libc.mips64.map b/libc/libc.mips64.map
index b7f7c89..3e20d7e 100644
--- a/libc/libc.mips64.map
+++ b/libc/libc.mips64.map
@@ -1344,6 +1344,7 @@
 LIBC_Q { # introduced=Q
   global:
     __res_randomid;
+    timespec_get;
 } LIBC_P;
 
 LIBC_PRIVATE {
diff --git a/libc/libc.x86.map b/libc/libc.x86.map
index ae7de4b..ef4da9e 100644
--- a/libc/libc.x86.map
+++ b/libc/libc.x86.map
@@ -1405,6 +1405,7 @@
 LIBC_Q { # introduced=Q
   global:
     __res_randomid;
+    timespec_get;
 } LIBC_P;
 
 LIBC_PRIVATE {
diff --git a/libc/libc.x86_64.map b/libc/libc.x86_64.map
index b7f7c89..3e20d7e 100644
--- a/libc/libc.x86_64.map
+++ b/libc/libc.x86_64.map
@@ -1344,6 +1344,7 @@
 LIBC_Q { # introduced=Q
   global:
     __res_randomid;
+    timespec_get;
 } LIBC_P;
 
 LIBC_PRIVATE {
diff --git a/tests/time_test.cpp b/tests/time_test.cpp
index 3196f34..216f846 100644
--- a/tests/time_test.cpp
+++ b/tests/time_test.cpp
@@ -922,3 +922,13 @@
   struct tm tm;
   ASSERT_EQ(nullptr, strptime("x", "%s", &tm));
 }
+
+TEST(time, timespec_get) {
+#if __BIONIC__
+  timespec ts = {};
+  ASSERT_EQ(0, timespec_get(&ts, 123));
+  ASSERT_EQ(TIME_UTC, timespec_get(&ts, TIME_UTC));
+#else
+  GTEST_LOG_(INFO) << "glibc doesn't have timespec_get until 2.21\n";
+#endif
+}