time.h revision 289644
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: head/sys/ofed/include/linux/time.h 289644 2015-10-20 19:08:26Z hselasky $ 27289572Shselasky */ 28289572Shselasky#ifndef _LINUX_TIME_H_ 29289572Shselasky#define _LINUX_TIME_H_ 30289572Shselasky 31289572Shselasky#define NSEC_PER_USEC 1000L 32289572Shselasky#define NSEC_PER_SEC 1000000000L 33289572Shselasky 34289572Shselasky#include <sys/time.h> 35289572Shselasky#include <sys/stdint.h> 36289572Shselasky 37289572Shselaskystatic inline struct timeval 38289572Shselaskyns_to_timeval(const int64_t nsec) 39289572Shselasky{ 40289572Shselasky struct timeval tv; 41289572Shselasky long rem; 42289572Shselasky 43289572Shselasky if (nsec == 0) { 44289572Shselasky tv.tv_sec = 0; 45289572Shselasky tv.tv_usec = 0; 46289572Shselasky return (tv); 47289572Shselasky } 48289572Shselasky 49289572Shselasky tv.tv_sec = nsec / NSEC_PER_SEC; 50289572Shselasky rem = nsec % NSEC_PER_SEC; 51289572Shselasky if (rem < 0) { 52289572Shselasky tv.tv_sec--; 53289572Shselasky rem += NSEC_PER_SEC; 54289572Shselasky } 55289572Shselasky tv.tv_usec = rem / 1000; 56289572Shselasky return (tv); 57289572Shselasky} 58289572Shselasky 59289572Shselaskystatic inline int64_t 60289572Shselaskytimeval_to_ns(const struct timeval *tv) 61289572Shselasky{ 62289572Shselasky return ((int64_t)tv->tv_sec * NSEC_PER_SEC) + 63289572Shselasky tv->tv_usec * NSEC_PER_USEC; 64289572Shselasky} 65289572Shselasky 66289572Shselasky#define getrawmonotonic(ts) nanouptime(ts) 67289572Shselasky 68289572Shselaskystatic inline struct timespec 69289572Shselaskytimespec_sub(struct timespec lhs, struct timespec rhs) 70289572Shselasky{ 71289572Shselasky struct timespec ts; 72289572Shselasky 73289572Shselasky ts.tv_sec = lhs.tv_sec; 74289572Shselasky ts.tv_nsec = lhs.tv_nsec; 75289572Shselasky timespecsub(&ts, &rhs); 76289572Shselasky 77289572Shselasky return ts; 78289572Shselasky} 79289572Shselasky 80289572Shselaskystatic inline void 81289572Shselaskyset_normalized_timespec(struct timespec *ts, time_t sec, int64_t nsec) 82289572Shselasky{ 83289572Shselasky /* XXX: this doesn't actually normalize anything */ 84289572Shselasky ts->tv_sec = sec; 85289572Shselasky ts->tv_nsec = nsec; 86289572Shselasky} 87289572Shselasky 88289572Shselaskystatic inline int64_t 89289572Shselaskytimespec_to_ns(const struct timespec *ts) 90289572Shselasky{ 91289572Shselasky return ((ts->tv_sec * NSEC_PER_SEC) + ts->tv_nsec); 92289572Shselasky} 93289572Shselasky 94289572Shselaskystatic inline struct timespec 95289572Shselaskyns_to_timespec(const int64_t nsec) 96289572Shselasky{ 97289572Shselasky struct timespec ts; 98289572Shselasky int32_t rem; 99289572Shselasky 100289572Shselasky if (nsec == 0) { 101289572Shselasky ts.tv_sec = 0; 102289572Shselasky ts.tv_nsec = 0; 103289572Shselasky return (ts); 104289572Shselasky } 105289572Shselasky 106289572Shselasky ts.tv_sec = nsec / NSEC_PER_SEC; 107289572Shselasky rem = nsec % NSEC_PER_SEC; 108289572Shselasky if (rem < 0) { 109289572Shselasky ts.tv_sec--; 110289572Shselasky rem += NSEC_PER_SEC; 111289572Shselasky } 112289572Shselasky ts.tv_nsec = rem; 113289572Shselasky return (ts); 114289572Shselasky} 115289572Shselasky 116289572Shselaskystatic inline int 117289572Shselaskytimespec_valid(const struct timespec *ts) 118289572Shselasky{ 119289572Shselasky if (ts->tv_sec < 0 || ts->tv_sec > 100000000 || 120289572Shselasky ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000) 121289572Shselasky return (0); 122289572Shselasky return (1); 123289572Shselasky} 124289572Shselasky 125289572Shselaskystatic inline unsigned long 126289572Shselaskyget_seconds(void) 127289572Shselasky{ 128289572Shselasky return time_uptime; 129289572Shselasky} 130289572Shselasky 131289572Shselasky#endif /* _LINUX_TIME_H_ */ 132