2020-06-15 07:18:57 -07:00
|
|
|
#ifndef COSMOPOLITAN_LIBC_CALLS_STRUCT_TIMEVAL_H_
|
|
|
|
#define COSMOPOLITAN_LIBC_CALLS_STRUCT_TIMEVAL_H_
|
2022-08-18 15:43:03 -07:00
|
|
|
#include "libc/calls/struct/timespec.h"
|
2024-05-04 23:05:36 -07:00
|
|
|
#include "libc/time.h"
|
2022-08-13 13:11:56 -07:00
|
|
|
COSMOPOLITAN_C_START_
|
2020-06-15 07:18:57 -07:00
|
|
|
|
|
|
|
struct timeval {
|
|
|
|
int64_t tv_sec;
|
|
|
|
int64_t tv_usec; /* microseconds */
|
|
|
|
};
|
|
|
|
|
2022-08-13 13:11:56 -07:00
|
|
|
int futimes(int, const struct timeval[2]);
|
|
|
|
int futimesat(int, const char *, const struct timeval[2]);
|
|
|
|
int gettimeofday(struct timeval *, struct timezone *);
|
2023-06-18 04:02:01 -07:00
|
|
|
int settimeofday(const struct timeval *, const struct timezone *);
|
2022-06-08 20:01:28 -07:00
|
|
|
int lutimes(const char *, const struct timeval[2]);
|
2022-08-13 13:11:56 -07:00
|
|
|
int utimes(const char *, const struct timeval[2]);
|
|
|
|
|
2023-08-13 20:31:27 -07:00
|
|
|
#ifdef _COSMO_SOURCE
|
2023-06-05 00:37:25 -07:00
|
|
|
/* cosmopolitan libc's non-posix timevals library
|
|
|
|
removed by default due to emacs codebase clash */
|
2023-06-15 13:50:42 -07:00
|
|
|
#define timeval_zero ((struct timeval){0})
|
|
|
|
#define timeval_max ((struct timeval){0x7fffffffffffffff, 999999})
|
2022-11-05 19:49:41 -07:00
|
|
|
int timeval_cmp(struct timeval, struct timeval) pureconst;
|
2023-06-15 13:50:42 -07:00
|
|
|
struct timeval timeval_real(void);
|
2022-11-07 02:22:09 -08:00
|
|
|
struct timeval timeval_frommicros(int64_t) pureconst;
|
|
|
|
struct timeval timeval_frommillis(int64_t) pureconst;
|
2022-11-05 19:49:41 -07:00
|
|
|
struct timeval timeval_add(struct timeval, struct timeval) pureconst;
|
|
|
|
struct timeval timeval_sub(struct timeval, struct timeval) pureconst;
|
2023-06-15 13:50:42 -07:00
|
|
|
struct timeval timeval_subz(struct timeval, struct timeval) pureconst;
|
2023-08-08 04:00:29 -07:00
|
|
|
int64_t timeval_toseconds(struct timeval);
|
2023-08-21 02:28:24 -07:00
|
|
|
int64_t timeval_tomicros(struct timeval);
|
2023-09-07 16:03:19 -07:00
|
|
|
int64_t timeval_tomillis(struct timeval);
|
2022-11-05 19:49:41 -07:00
|
|
|
struct timeval timespec_totimeval(struct timespec) pureconst;
|
2023-08-08 04:00:29 -07:00
|
|
|
static inline struct timeval timeval_fromseconds(int64_t __x) {
|
|
|
|
return (struct timeval){__x};
|
|
|
|
}
|
2023-08-07 20:22:49 -07:00
|
|
|
static inline struct timespec timeval_totimespec(struct timeval __tv) {
|
|
|
|
return (struct timespec){__tv.tv_sec, __tv.tv_usec * 1000};
|
|
|
|
}
|
2023-11-06 16:38:44 -08:00
|
|
|
static inline int timeval_iszero(struct timeval __tv) {
|
2023-06-15 13:50:42 -07:00
|
|
|
return !(__tv.tv_sec | __tv.tv_usec);
|
|
|
|
}
|
2023-11-06 16:38:44 -08:00
|
|
|
static inline int timeval_isvalid(struct timeval __tv) {
|
2023-09-01 20:49:13 -07:00
|
|
|
return __tv.tv_sec >= 0 && __tv.tv_usec + 0ull < 1000000ull;
|
2023-06-15 13:50:42 -07:00
|
|
|
}
|
2023-08-13 20:31:27 -07:00
|
|
|
#endif /* _COSMO_SOURCE */
|
2022-06-08 20:01:28 -07:00
|
|
|
|
2023-10-31 21:58:49 -07:00
|
|
|
#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
|
|
|
|
|
2022-08-13 13:11:56 -07:00
|
|
|
COSMOPOLITAN_C_END_
|
2020-06-15 07:18:57 -07:00
|
|
|
#endif /* COSMOPOLITAN_LIBC_CALLS_STRUCT_TIMEVAL_H_ */
|