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