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