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