1289625Shselasky/*- 2289572Shselasky * Copyright (c) 2014-2015 Fran��ois Tigeot 3289572Shselasky * All rights reserved. 4289572Shselasky * 5289572Shselasky * Redistribution and use in source and binary forms, with or without 6289572Shselasky * modification, are permitted provided that the following conditions 7289572Shselasky * are met: 8289572Shselasky * 1. Redistributions of source code must retain the above copyright 9289572Shselasky * notice unmodified, this list of conditions, and the following 10289572Shselasky * disclaimer. 11289572Shselasky * 2. Redistributions in binary form must reproduce the above copyright 12289572Shselasky * notice, this list of conditions and the following disclaimer in the 13289572Shselasky * documentation and/or other materials provided with the distribution. 14289572Shselasky * 15289572Shselasky * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16289572Shselasky * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17289572Shselasky * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18289572Shselasky * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19289572Shselasky * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20289572Shselasky * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21289572Shselasky * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22289572Shselasky * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23289572Shselasky * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24289572Shselasky * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25289644Shselasky * 26289644Shselasky * $FreeBSD: stable/11/sys/compat/linuxkpi/common/include/linux/time.h 335429 2018-06-20 06:52:32Z hselasky $ 27289572Shselasky */ 28289572Shselasky#ifndef _LINUX_TIME_H_ 29289572Shselasky#define _LINUX_TIME_H_ 30289572Shselasky 31289572Shselasky#define NSEC_PER_USEC 1000L 32300507Shselasky#define NSEC_PER_MSEC 1000000L 33289572Shselasky#define NSEC_PER_SEC 1000000000L 34289572Shselasky 35333323Shselasky#define USEC_PER_MSEC 1000L 36333323Shselasky#define USEC_PER_SEC 1000000L 37333323Shselasky 38335429Shselasky#define timespec64 timespec 39335429Shselasky 40289572Shselasky#include <sys/time.h> 41289572Shselasky#include <sys/stdint.h> 42289572Shselasky 43289572Shselaskystatic inline struct timeval 44289572Shselaskyns_to_timeval(const int64_t nsec) 45289572Shselasky{ 46289572Shselasky struct timeval tv; 47289572Shselasky long rem; 48289572Shselasky 49289572Shselasky if (nsec == 0) { 50289572Shselasky tv.tv_sec = 0; 51289572Shselasky tv.tv_usec = 0; 52289572Shselasky return (tv); 53289572Shselasky } 54289572Shselasky 55289572Shselasky tv.tv_sec = nsec / NSEC_PER_SEC; 56289572Shselasky rem = nsec % NSEC_PER_SEC; 57289572Shselasky if (rem < 0) { 58289572Shselasky tv.tv_sec--; 59289572Shselasky rem += NSEC_PER_SEC; 60289572Shselasky } 61289572Shselasky tv.tv_usec = rem / 1000; 62289572Shselasky return (tv); 63289572Shselasky} 64289572Shselasky 65289572Shselaskystatic inline int64_t 66289572Shselaskytimeval_to_ns(const struct timeval *tv) 67289572Shselasky{ 68289572Shselasky return ((int64_t)tv->tv_sec * NSEC_PER_SEC) + 69289572Shselasky tv->tv_usec * NSEC_PER_USEC; 70289572Shselasky} 71289572Shselasky 72289572Shselasky#define getrawmonotonic(ts) nanouptime(ts) 73289572Shselasky 74289572Shselaskystatic inline struct timespec 75289572Shselaskytimespec_sub(struct timespec lhs, struct timespec rhs) 76289572Shselasky{ 77289572Shselasky struct timespec ts; 78289572Shselasky 79289572Shselasky ts.tv_sec = lhs.tv_sec; 80289572Shselasky ts.tv_nsec = lhs.tv_nsec; 81289572Shselasky timespecsub(&ts, &rhs); 82289572Shselasky 83289572Shselasky return ts; 84289572Shselasky} 85289572Shselasky 86289572Shselaskystatic inline void 87289572Shselaskyset_normalized_timespec(struct timespec *ts, time_t sec, int64_t nsec) 88289572Shselasky{ 89289572Shselasky /* XXX: this doesn't actually normalize anything */ 90289572Shselasky ts->tv_sec = sec; 91289572Shselasky ts->tv_nsec = nsec; 92289572Shselasky} 93289572Shselasky 94289572Shselaskystatic inline int64_t 95289572Shselaskytimespec_to_ns(const struct timespec *ts) 96289572Shselasky{ 97289572Shselasky return ((ts->tv_sec * NSEC_PER_SEC) + ts->tv_nsec); 98289572Shselasky} 99289572Shselasky 100289572Shselaskystatic inline struct timespec 101289572Shselaskyns_to_timespec(const int64_t nsec) 102289572Shselasky{ 103289572Shselasky struct timespec ts; 104289572Shselasky int32_t rem; 105289572Shselasky 106289572Shselasky if (nsec == 0) { 107289572Shselasky ts.tv_sec = 0; 108289572Shselasky ts.tv_nsec = 0; 109289572Shselasky return (ts); 110289572Shselasky } 111289572Shselasky 112289572Shselasky ts.tv_sec = nsec / NSEC_PER_SEC; 113289572Shselasky rem = nsec % NSEC_PER_SEC; 114289572Shselasky if (rem < 0) { 115289572Shselasky ts.tv_sec--; 116289572Shselasky rem += NSEC_PER_SEC; 117289572Shselasky } 118289572Shselasky ts.tv_nsec = rem; 119289572Shselasky return (ts); 120289572Shselasky} 121289572Shselasky 122289572Shselaskystatic inline int 123289572Shselaskytimespec_valid(const struct timespec *ts) 124289572Shselasky{ 125289572Shselasky if (ts->tv_sec < 0 || ts->tv_sec > 100000000 || 126289572Shselasky ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000) 127289572Shselasky return (0); 128289572Shselasky return (1); 129289572Shselasky} 130289572Shselasky 131289572Shselaskystatic inline unsigned long 132289572Shselaskyget_seconds(void) 133289572Shselasky{ 134289572Shselasky return time_uptime; 135289572Shselasky} 136289572Shselasky 137289572Shselasky#endif /* _LINUX_TIME_H_ */ 138