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