time.h revision 1.7
1/*	$NetBSD: time.h,v 1.7 2021/12/19 01:21:45 riastradh Exp $	*/
2
3/*-
4 * Copyright (c) 2013 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Taylor R. Campbell.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#ifndef _LINUX_TIME_H_
33#define _LINUX_TIME_H_
34
35#include <sys/time.h>
36
37#include <linux/math64.h>
38
39#define	timespec64	timespec	/* take that, 2038 */
40
41#define NSEC_PER_MSEC	1000000L
42#define	NSEC_PER_SEC	1000000000L
43
44/*
45 * XXX get_seconds as implemented by Linux is a Y2038 bug waiting to
46 * happen on 32-bit systems because it returns unsigned long.  Some
47 * callers in Linux (implicitly) convert the result to time_t, though.
48 * We'll pretend get_seconds returns time_t and make sure all our
49 * callers treat it as if it did.
50 */
51
52static inline time_t
53get_seconds(void)
54{
55	return time_second;
56}
57
58static inline void
59getrawmonotonic(struct timespec *ts)
60{
61	getnanouptime(ts);
62}
63
64static inline void
65do_gettimeofday(struct timeval *tv)
66{
67	microtime(tv);
68}
69
70static inline bool
71timespec_valid(const struct timespec *ts)
72{
73	if (ts->tv_sec < 0)
74		return false;
75	if (1000000000L <= ts->tv_nsec)
76		return false;
77	return true;
78}
79
80static inline struct timespec
81ns_to_timespec(const int64_t nsec)
82{
83	struct timespec ts;
84
85	ts.tv_sec = (nsec / 1000000000L);
86	ts.tv_nsec = (nsec % 1000000000L);
87	if (ts.tv_nsec < 0) {
88		ts.tv_sec -= 1;
89		ts.tv_nsec += 1000000000L;
90	}
91
92	return ts;
93}
94
95static inline int64_t
96timespec_to_ns(const struct timespec *ts)
97{
98	return (((int64_t)ts->tv_sec * 1000000000LL) + ts->tv_nsec);
99}
100
101static inline struct timeval
102ns_to_timeval(int64_t nsec)
103{
104	struct timespec ts;
105	struct timeval tv;
106
107	ts = ns_to_timespec(nsec);
108	TIMESPEC_TO_TIMEVAL(&tv, &ts);
109
110	return tv;
111}
112
113static inline int64_t
114timeval_to_ns(const struct timeval *tv)
115{
116	return (((int64_t)tv->tv_sec * 1000000000UL) + (tv->tv_usec * 1000ul));
117}
118
119static inline struct timespec
120timespec_sub(struct timespec a, struct timespec b)
121{
122	struct timespec d;
123
124	timespecsub(&a, &b, &d);
125
126	return d;
127}
128
129static inline void
130set_normalized_timespec(struct timespec *ts, time_t sec, int64_t nsec)
131{
132	while (nsec >= 1000000000L) {
133		nsec -= 1000000000L;
134		sec += 1;
135	}
136	while (nsec < 0) {
137		nsec += 1000000000L;
138		sec -= 1;
139	}
140	ts->tv_sec = sec;
141	ts->tv_nsec = nsec;
142}
143
144#endif  /* _LINUX_TIME_H_ */
145