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