xref: /freebsd/sys/compat/linuxkpi/common/include/linux/time.h (revision 592ae60e2b2eff6c2ec467c34be9a457ce24b044)
1 /*-
2  * Copyright (c) 2014-2015 François Tigeot
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice unmodified, this list of conditions, and the following
10  *    disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 #ifndef _LINUXKPI_LINUX_TIME_H_
27 #define	_LINUXKPI_LINUX_TIME_H_
28 
29 #define	MSEC_PER_SEC	1000L
30 
31 #define	NSEC_PER_USEC	1000L
32 #define	NSEC_PER_MSEC	1000000L
33 #define	NSEC_PER_SEC	1000000000L
34 
35 #define	USEC_PER_MSEC	1000L
36 #define	USEC_PER_SEC	1000000L
37 
38 #define	timespec64 timespec
39 
40 #include <sys/time.h>
41 #include <sys/stdint.h>
42 
43 #include <linux/math64.h>
44 
45 typedef int64_t time64_t;
46 
47 /* Copied from `include/time.h` (userland). */
48 struct tm {
49 	int	tm_sec;		/* seconds after the minute [0-60] */
50 	int	tm_min;		/* minutes after the hour [0-59] */
51 	int	tm_hour;	/* hours since midnight [0-23] */
52 	int	tm_mday;	/* day of the month [1-31] */
53 	int	tm_mon;		/* months since January [0-11] */
54 	int	tm_year;	/* years since 1900 */
55 	int	tm_wday;	/* days since Sunday [0-6] */
56 	int	tm_yday;	/* days since January 1 [0-365] */
57 	int	tm_isdst;	/* Daylight Savings Time flag */
58 	long	tm_gmtoff;	/* offset from UTC in seconds */
59 	char	*tm_zone;	/* timezone abbreviation */
60 };
61 
62 static inline struct timeval
63 ns_to_timeval(const int64_t nsec)
64 {
65 	struct timeval tv;
66 	long rem;
67 
68 	if (nsec == 0) {
69 		tv.tv_sec = 0;
70 		tv.tv_usec = 0;
71 		return (tv);
72 	}
73 
74 	tv.tv_sec = nsec / NSEC_PER_SEC;
75 	rem = nsec % NSEC_PER_SEC;
76 	if (rem < 0) {
77 		tv.tv_sec--;
78 		rem += NSEC_PER_SEC;
79 	}
80 	tv.tv_usec = rem / 1000;
81 	return (tv);
82 }
83 
84 static inline int64_t
85 timeval_to_ns(const struct timeval *tv)
86 {
87 	return ((int64_t)tv->tv_sec * NSEC_PER_SEC) +
88 		tv->tv_usec * NSEC_PER_USEC;
89 }
90 
91 #define getrawmonotonic(ts)	nanouptime(ts)
92 
93 static inline struct timespec
94 timespec_sub(struct timespec lhs, struct timespec rhs)
95 {
96 	struct timespec ts;
97 
98 	timespecsub(&lhs, &rhs, &ts);
99 
100 	return ts;
101 }
102 
103 static inline void
104 set_normalized_timespec(struct timespec *ts, time_t sec, int64_t nsec)
105 {
106 	/* XXX: this doesn't actually normalize anything */
107 	ts->tv_sec = sec;
108 	ts->tv_nsec = nsec;
109 }
110 
111 static inline int64_t
112 timespec_to_ns(const struct timespec *ts)
113 {
114 	return ((ts->tv_sec * NSEC_PER_SEC) + ts->tv_nsec);
115 }
116 
117 static inline struct timespec
118 ns_to_timespec(const int64_t nsec)
119 {
120 	struct timespec ts;
121 	int32_t rem;
122 
123 	if (nsec == 0) {
124 		ts.tv_sec = 0;
125 		ts.tv_nsec = 0;
126 		return (ts);
127 	}
128 
129 	ts.tv_sec = nsec / NSEC_PER_SEC;
130 	rem = nsec % NSEC_PER_SEC;
131 	if (rem < 0) {
132 		ts.tv_sec--;
133 		rem += NSEC_PER_SEC;
134 	}
135 	ts.tv_nsec = rem;
136 	return (ts);
137 }
138 
139 #define	ns_to_timespec64(_x)	ns_to_timespec(_x)
140 
141 static inline int
142 timespec_valid(const struct timespec *ts)
143 {
144 	if (ts->tv_sec < 0 || ts->tv_sec > 100000000 ||
145 	    ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000)
146 		return (0);
147 	return (1);
148 }
149 
150 static inline unsigned long
151 get_seconds(void)
152 {
153 	return time_uptime;
154 }
155 
156 void linuxkpi_time64_to_tm(time64_t totalsecs, int offset, struct tm *result);
157 #define time64_to_tm(totalsecs, offset, result) \
158     linuxkpi_time64_to_tm(totalsecs, offset, result)
159 
160 #endif /* _LINUXKPI_LINUX_TIME_H_ */
161