/* $OpenBSD: time.h,v 1.1 2019/04/14 10:14:53 jsg Exp $ */ /* * Copyright (c) 2013, 2014, 2015 Mark Kettenis * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ #ifndef _LINUX_TIME_H #define _LINUX_TIME_H #include #include #define NSEC_PER_USEC 1000L #define NSEC_PER_MSEC 1000000L #define NSEC_PER_SEC 1000000000L extern struct timespec ns_to_timespec(const int64_t); extern int64_t timeval_to_ms(const struct timeval *); extern int64_t timeval_to_ns(const struct timeval *); extern int64_t timeval_to_us(const struct timeval *); extern struct timeval ns_to_timeval(const int64_t); struct timespec64 { time_t tv_sec; long tv_nsec; }; static inline struct timespec timespec_sub(struct timespec t1, struct timespec t2) { struct timespec diff; timespecsub(&t1, &t2, &diff); return diff; } static inline void set_normalized_timespec(struct timespec *ts, time_t sec, int64_t nsec) { while (nsec > NSEC_PER_SEC) { nsec -= NSEC_PER_SEC; sec++; } ts->tv_sec = sec; ts->tv_nsec = nsec; } static inline int64_t timespec_to_ns(const struct timespec *ts) { return ((ts->tv_sec * NSEC_PER_SEC) + ts->tv_nsec); } static inline int timespec_valid(const struct timespec *ts) { if (ts->tv_sec < 0 || ts->tv_sec > 100000000 || ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000) return (0); return (1); } #endif