1
2#ifndef _LINUX_TIME64_H
3#define _LINUX_TIME64_H
4
5#include <linux/math64.h>
6
7typedef __s64 time64_t;
8typedef __u64 timeu64_t;
9
10#include <uapi/linux/time.h>
11
12struct timespec64 {
13 time64_t tv_sec;
14 long tv_nsec;
15};
16
17struct itimerspec64 {
18 struct timespec64 it_interval;
19 struct timespec64 it_value;
20};
21
22
23#define MSEC_PER_SEC 1000L
24#define USEC_PER_MSEC 1000L
25#define NSEC_PER_USEC 1000L
26#define NSEC_PER_MSEC 1000000L
27#define USEC_PER_SEC 1000000L
28#define NSEC_PER_SEC 1000000000L
29#define FSEC_PER_SEC 1000000000000000LL
30
31
32#define TIME64_MAX ((s64)~((u64)1 << 63))
33#define KTIME_MAX ((s64)~((u64)1 << 63))
34#define KTIME_SEC_MAX (KTIME_MAX / NSEC_PER_SEC)
35
36
37
38
39
40
41
42
43
44#define TIME_UPTIME_SEC_MAX (30LL * 365 * 24 *3600)
45#define TIME_SETTOD_SEC_MAX (KTIME_SEC_MAX - TIME_UPTIME_SEC_MAX)
46
47static inline int timespec64_equal(const struct timespec64 *a,
48 const struct timespec64 *b)
49{
50 return (a->tv_sec == b->tv_sec) && (a->tv_nsec == b->tv_nsec);
51}
52
53
54
55
56
57
58static inline int timespec64_compare(const struct timespec64 *lhs, const struct timespec64 *rhs)
59{
60 if (lhs->tv_sec < rhs->tv_sec)
61 return -1;
62 if (lhs->tv_sec > rhs->tv_sec)
63 return 1;
64 return lhs->tv_nsec - rhs->tv_nsec;
65}
66
67extern void set_normalized_timespec64(struct timespec64 *ts, time64_t sec, s64 nsec);
68
69static inline struct timespec64 timespec64_add(struct timespec64 lhs,
70 struct timespec64 rhs)
71{
72 struct timespec64 ts_delta;
73 set_normalized_timespec64(&ts_delta, lhs.tv_sec + rhs.tv_sec,
74 lhs.tv_nsec + rhs.tv_nsec);
75 return ts_delta;
76}
77
78
79
80
81static inline struct timespec64 timespec64_sub(struct timespec64 lhs,
82 struct timespec64 rhs)
83{
84 struct timespec64 ts_delta;
85 set_normalized_timespec64(&ts_delta, lhs.tv_sec - rhs.tv_sec,
86 lhs.tv_nsec - rhs.tv_nsec);
87 return ts_delta;
88}
89
90
91
92
93static inline bool timespec64_valid(const struct timespec64 *ts)
94{
95
96 if (ts->tv_sec < 0)
97 return false;
98
99 if ((unsigned long)ts->tv_nsec >= NSEC_PER_SEC)
100 return false;
101 return true;
102}
103
104static inline bool timespec64_valid_strict(const struct timespec64 *ts)
105{
106 if (!timespec64_valid(ts))
107 return false;
108
109 if ((unsigned long long)ts->tv_sec >= KTIME_SEC_MAX)
110 return false;
111 return true;
112}
113
114static inline bool timespec64_valid_settod(const struct timespec64 *ts)
115{
116 if (!timespec64_valid(ts))
117 return false;
118
119 if ((unsigned long long)ts->tv_sec >= TIME_SETTOD_SEC_MAX)
120 return false;
121 return true;
122}
123
124
125
126
127
128
129
130
131static inline s64 timespec64_to_ns(const struct timespec64 *ts)
132{
133 return ((s64) ts->tv_sec * NSEC_PER_SEC) + ts->tv_nsec;
134}
135
136
137
138
139
140
141
142extern struct timespec64 ns_to_timespec64(const s64 nsec);
143
144
145
146
147
148
149
150
151
152static __always_inline void timespec64_add_ns(struct timespec64 *a, u64 ns)
153{
154 a->tv_sec += __iter_div_u64_rem(a->tv_nsec + ns, NSEC_PER_SEC, &ns);
155 a->tv_nsec = ns;
156}
157
158
159
160
161
162extern struct timespec64 timespec64_add_safe(const struct timespec64 lhs,
163 const struct timespec64 rhs);
164
165#endif
166