[PATCH v2 2/4] time: Implement c23 timespec_get base

Yonggang Luo luoyonggang@gmail.com
Tue Jun 20 17:17:29 GMT 2023


These are implemented https://gustedt.gitlabpages.inria.fr/c23-library/#time_monotonic-time_active-time_thread_active

with:

# define TIME_MONOTONIC          2
# define TIME_ACTIVE             3
# define TIME_THREAD_ACTIVE      4
# define TIME_MONOTONIC_RAW      5
# define TIME_UTC_COARSE         6
# define TIME_MONOTONIC_COARSE   7
# define TIME_BOOTTIME           8
# define TIME_UTC_ALARM          9
# define TIME_BOOTTIME_ALARM     10
# define TIME_SGI_CYCLE          11
# define TIME_TAI                12

Signed-off-by: Yonggang Luo <luoyonggang@gmail.com>
---
 time/time.h         | 13 +++++++++++-
 time/timespec_get.c | 51 +++++++++++++++++++++++++++++++++++++++++----
 2 files changed, 59 insertions(+), 5 deletions(-)

diff --git a/time/time.h b/time/time.h
index 368f4dc588..660cbb200b 100644
--- a/time/time.h
+++ b/time/time.h
@@ -62,7 +62,18 @@ typedef __pid_t pid_t;
 
 #ifdef __USE_ISOC11
 /* Time base values for timespec_get.  */
-# define TIME_UTC 1
+# define TIME_UTC                1
+# define TIME_MONOTONIC          2
+# define TIME_ACTIVE             3
+# define TIME_THREAD_ACTIVE      4
+# define TIME_MONOTONIC_RAW      5
+# define TIME_UTC_COARSE         6
+# define TIME_MONOTONIC_COARSE   7
+# define TIME_BOOTTIME           8
+# define TIME_UTC_ALARM          9
+# define TIME_BOOTTIME_ALARM     10
+# define TIME_SGI_CYCLE          11
+# define TIME_TAI                12
 #endif
 
 __BEGIN_DECLS
diff --git a/time/timespec_get.c b/time/timespec_get.c
index 9b1d4f22ed..20c0e4d9aa 100644
--- a/time/timespec_get.c
+++ b/time/timespec_get.c
@@ -17,15 +17,58 @@
 
 #include <time.h>
 
-
 /* Set TS to calendar time based in time base BASE.  */
 int
 timespec_get (struct timespec *ts, int base)
 {
-  if (base == TIME_UTC)
+  clockid_t clockid = -1;
+  switch (base)
+    {
+    default:
+      break;
+    case TIME_UTC:
+      clockid = CLOCK_REALTIME;
+      break;
+    case TIME_MONOTONIC:
+      clockid = CLOCK_MONOTONIC;
+      break;
+    case TIME_ACTIVE:
+      clockid = CLOCK_PROCESS_CPUTIME_ID;
+      break;
+    case TIME_THREAD_ACTIVE:
+      clockid = CLOCK_THREAD_CPUTIME_ID;
+      break;
+    case TIME_MONOTONIC_RAW:
+      clockid = CLOCK_MONOTONIC_RAW;
+      break;
+    case TIME_UTC_COARSE:
+      clockid = CLOCK_REALTIME_COARSE;
+      break;
+    case TIME_MONOTONIC_COARSE:
+      clockid = CLOCK_MONOTONIC_COARSE;
+      break;
+    case TIME_BOOTTIME:
+      clockid = CLOCK_BOOTTIME;
+      break;
+    case TIME_UTC_ALARM:
+      clockid = CLOCK_REALTIME_ALARM;
+      break;
+    case TIME_BOOTTIME_ALARM:
+      clockid = CLOCK_BOOTTIME_ALARM;
+      break;
+    case TIME_SGI_CYCLE:
+      clockid = CLOCK_SGI_CYCLE;
+      break;
+    case TIME_TAI:
+      clockid = CLOCK_TAI;
+      break;
+    }
+  if (clockid >= 0)
     {
-      __clock_gettime (CLOCK_REALTIME, ts);
-      return base;
+      if (__clock_gettime (clockid, ts) >= 0)
+        {
+          return base;
+        }
     }
   return 0;
 }
-- 
2.39.0.windows.1



More information about the Libc-alpha mailing list