Skip to content

include/sys/time.h: Add timespecadd, timespecsub, and timespeccmp - #21115

Merged
acassis merged 1 commit into
apache:masterfrom
Swatantra-66:libc/timespec-macros
Oct 30, 2026
Merged

acassis merged 1 commit into
apache:masterfrom
Swatantra-66:libc/timespec-macros

Conversation

@Swatantra-66

Copy link
Copy Markdown
Contributor

Summary

<sys/time.h> defines standard BSD/Linux convenience macros timeradd, timersub, timerclear, timerisset, and timercmp for operations on struct timeval.

Most standard C libraries (glibc, musl, FreeBSD, NetBSD, OpenBSD) provide the corresponding operations for struct timespec: timespecadd, timespecsub, timespecclear, timespecisset, and timespeccmp.

This patch implements these standard convenience macros in include/sys/time.h mirroring the existing timeval implementations, improving BSD/POSIX portability and compatibility.

Impact

  • Users/Developers: Adds standard timespec arithmetic and comparison macros in <sys/time.h>.
  • Compatibility: Backward compatible. No existing APIs, ABI, or behaviors modified.
  • Build/Hardware: No impact on existing builds.

Testing

  • Verified syntax, header inclusion (<time.h> is already included in include/sys/time.h), and clean compilation.
  • Verified style adherence via tools/checkpatch.sh:
    • 0 errors, 0 warnings.
    • All lines within standard 78-column limit.
    • No trailing whitespace.
  • Verified macro behavior across edge cases (carry on addition at boundary $10^9\text{ ns}$, borrow on subtraction, equality, zero clearing, relational comparison operators).

<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]>
@github-actions github-actions Bot added the Size: S The size of the change in this PR is small label Oct 29, 2026
@github-actions

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

No memory changes detected for:


Catch memory regressions in CI with MemBrowse. Free for open source.

@acassis
acassis merged commit 5bf15a0 into apache:master Oct 30, 2026
53 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Size: S The size of the change in this PR is small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants