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