1 | /* $NetBSD: time.h,v 1.3 2014/07/16 20:56:25 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 | /* |
38 | * XXX get_seconds as implemented by Linux is a Y2038 bug waiting to |
39 | * happen on 32-bit systems because it returns unsigned long. Some |
40 | * callers in Linux (implicitly) convert the result to time_t, though. |
41 | * We'll pretend get_seconds returns time_t and make sure all our |
42 | * callers treat it as if it did. |
43 | */ |
44 | |
45 | static inline time_t |
46 | get_seconds(void) |
47 | { |
48 | return time_second; |
49 | } |
50 | |
51 | static inline void |
52 | getrawmonotonic(struct timespec *ts) |
53 | { |
54 | getnanouptime(ts); |
55 | } |
56 | |
57 | static inline void |
58 | do_gettimeofday(struct timeval *tv) |
59 | { |
60 | microtime(tv); |
61 | } |
62 | |
63 | static inline bool |
64 | timespec_valid(const struct timespec *ts) |
65 | { |
66 | if (ts->tv_sec < 0) |
67 | return false; |
68 | if (1000000000L <= ts->tv_nsec) |
69 | return false; |
70 | return true; |
71 | } |
72 | |
73 | static inline struct timespec |
74 | ns_to_timespec(const int64_t nsec) |
75 | { |
76 | struct timespec ts; |
77 | |
78 | ts.tv_sec = (nsec / 1000000000L); |
79 | ts.tv_nsec = (nsec % 1000000000L); |
80 | if (ts.tv_nsec < 0) { |
81 | ts.tv_sec -= 1; |
82 | ts.tv_nsec += 1000000000L; |
83 | } |
84 | |
85 | return ts; |
86 | } |
87 | |
88 | static inline int64_t |
89 | timespec_to_ns(const struct timespec *ts) |
90 | { |
91 | return (((int64_t)ts->tv_sec * 1000000000LL) + ts->tv_nsec); |
92 | } |
93 | |
94 | static inline struct timeval |
95 | ns_to_timeval(int64_t nsec) |
96 | { |
97 | struct timespec ts; |
98 | struct timeval tv; |
99 | |
100 | ts = ns_to_timespec(nsec); |
101 | TIMESPEC_TO_TIMEVAL(&tv, &ts); |
102 | |
103 | return tv; |
104 | } |
105 | |
106 | static inline int64_t |
107 | timeval_to_ns(struct timeval *tv) |
108 | { |
109 | return (((int64_t)tv->tv_sec * 1000000000UL) + (tv->tv_usec * 1000ul)); |
110 | } |
111 | |
112 | static inline struct timespec |
113 | timespec_sub(struct timespec a, struct timespec b) |
114 | { |
115 | struct timespec d; |
116 | |
117 | timespecsub(&a, &b, &d); |
118 | |
119 | return d; |
120 | } |
121 | |
122 | static inline void |
123 | set_normalized_timespec(struct timespec *ts, time_t sec, int64_t nsec) |
124 | { |
125 | while (nsec >= 1000000000L) { |
126 | nsec -= 1000000000L; |
127 | sec += 1; |
128 | } |
129 | while (nsec < 0) { |
130 | nsec += 1000000000L; |
131 | sec -= 1; |
132 | } |
133 | ts->tv_sec = sec; |
134 | ts->tv_nsec = nsec; |
135 | } |
136 | |
137 | #endif /* _LINUX_TIME_H_ */ |
138 | |