This is an automated email from the ASF dual-hosted git repository.

acassis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new 5bf15a0f1a8 include/sys/time.h: Add timespecadd, timespecsub, and 
timespeccmp.
5bf15a0f1a8 is described below

commit 5bf15a0f1a8f1bbc5ff9de5d11a1747ccaf31e94
Author: Swatantra Yadav <[email protected]>
AuthorDate: Thu Oct 29 14:42:58 2026 +0530

    include/sys/time.h: Add timespecadd, timespecsub, and timespeccmp.
    
    <sys/time.h> defines timeradd, timersub, timerclear, timerisset, and
    timercmp for operations on struct timeval.  The BSDs, glibc and musl
    also provide the corresponding macros for operations on struct timespec:
    timespecadd, timespecsub, timespecclear, timespecisset, and timespeccmp.
    
    This patch adds these convenience macros to <sys/time.h> for improved
    standard POSIX/BSD compatibility.
    
    Assisted-by: Google Gemini
    Signed-off-by: Swatantra Yadav <[email protected]>
---
 include/sys/time.h | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 56 insertions(+)

diff --git a/include/sys/time.h b/include/sys/time.h
index 2a93e5ee736..a827695897e 100644
--- a/include/sys/time.h
+++ b/include/sys/time.h
@@ -104,6 +104,62 @@
    ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
    ((tvp)->tv_sec cmp (uvp)->tv_sec))
 
+/* void timespecadd(FAR struct timespec *a, FAR struct timespec *b,
+ *                  FAR struct timespec *res);
+ */
+
+#define timespecadd(tsp, usp, vsp) \
+  do \
+    { \
+      (vsp)->tv_sec = (tsp)->tv_sec + (usp)->tv_sec; \
+      (vsp)->tv_nsec = (tsp)->tv_nsec + (usp)->tv_nsec; \
+      if ((vsp)->tv_nsec >= 1000000000) \
+        { \
+          (vsp)->tv_sec++; \
+          (vsp)->tv_nsec -= 1000000000; \
+        } \
+    } \
+  while (0)
+
+/* void timespecsub(FAR struct timespec *a, FAR struct timespec *b,
+ *                  FAR struct timespec *res);
+ */
+
+#define timespecsub(tsp, usp, vsp) \
+  do \
+    { \
+      (vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec; \
+      (vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec; \
+      if ((vsp)->tv_nsec < 0) \
+        { \
+          (vsp)->tv_sec--; \
+          (vsp)->tv_nsec += 1000000000; \
+        } \
+    } \
+  while (0)
+
+/* void timespecclear(FAR struct timespec *tsp); */
+
+#define timespecclear(tsp) \
+  do \
+    { \
+      (tsp)->tv_sec = 0; \
+      (tsp)->tv_nsec = 0; \
+    } \
+  while (0)
+
+/* int timespecisset(FAR struct timespec *tsp); */
+
+#define timespecisset(tsp) \
+  ((tsp)->tv_sec != 0 || (tsp)->tv_nsec != 0)
+
+/* int timespeccmp(FAR struct timespec *a, FAR struct timespec *b, CMP); */
+
+#define timespeccmp(tsp, usp, cmp) \
+  (((tsp)->tv_sec == (usp)->tv_sec) ? \
+   ((tsp)->tv_nsec cmp (usp)->tv_nsec) : \
+   ((tsp)->tv_sec cmp (usp)->tv_sec))
+
 /* Macros for converting between `struct timeval' and `struct timespec'.  */
 
 #define TIMEVAL_TO_TIMESPEC(tv, ts) \

Reply via email to