Introduce GNU/BSD timeval macros

This commit is contained in:
Justine Tunney 2023-10-31 21:58:49 -07:00
parent 2af7c802b6
commit a1e1e821cb
No known key found for this signature in database
GPG key ID: BE714B4575D6E328

View file

@ -47,6 +47,29 @@ static inline bool timeval_isvalid(struct timeval __tv) {
}
#endif /* _COSMO_SOURCE */
#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
#define timerisset(t) ((t)->tv_sec || (t)->tv_usec)
#define timerclear(t) ((t)->tv_sec = (t)->tv_usec = 0)
#define timercmp(s, t, op) \
((s)->tv_sec == (t)->tv_sec ? (s)->tv_usec op(t)->tv_usec \
: (s)->tv_sec op(t)->tv_sec)
#define timeradd(s, t, a) \
(void)((a)->tv_sec = (s)->tv_sec + (t)->tv_sec, \
((a)->tv_usec = (s)->tv_usec + (t)->tv_usec) >= 1000000 && \
((a)->tv_usec -= 1000000, (a)->tv_sec++))
#define timersub(s, t, a) \
(void)((a)->tv_sec = (s)->tv_sec - (t)->tv_sec, \
((a)->tv_usec = (s)->tv_usec - (t)->tv_usec) < 0 && \
((a)->tv_usec += 1000000, (a)->tv_sec--))
#endif
#ifdef _GNU_SOURCE
#define TIMEVAL_TO_TIMESPEC(tv, ts) \
((ts)->tv_sec = (tv)->tv_sec, (ts)->tv_nsec = (tv)->tv_usec * 1000, (void)0)
#define TIMESPEC_TO_TIMEVAL(tv, ts) \
((tv)->tv_sec = (ts)->tv_sec, (tv)->tv_usec = (ts)->tv_nsec / 1000, (void)0)
#endif
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_CALLS_STRUCT_TIMEVAL_H_ */