1 #ifndef _LINUX_TIME32_H
2 #define _LINUX_TIME32_H
4 * These are all interfaces based on the old time_t definition
5 * that overflows in 2038 on 32-bit architectures. New code
6 * should use the replacements based on time64_t and timespec64.
8 * Any interfaces in here that become unused as we migrate
9 * code to time64_t should get removed.
12 #include <linux/time64.h>
14 #define TIME_T_MAX (time_t)((1UL << ((sizeof(time_t) << 3) - 1)) - 1)
16 #if __BITS_PER_LONG == 64
18 /* timespec64 is defined as timespec here */
19 static inline struct timespec
timespec64_to_timespec(const struct timespec64 ts64
)
24 static inline struct timespec64
timespec_to_timespec64(const struct timespec ts
)
29 # define timespec_equal timespec64_equal
30 # define timespec_compare timespec64_compare
31 # define set_normalized_timespec set_normalized_timespec64
32 # define timespec_add timespec64_add
33 # define timespec_sub timespec64_sub
34 # define timespec_valid timespec64_valid
35 # define timespec_valid_strict timespec64_valid_strict
36 # define timespec_to_ns timespec64_to_ns
37 # define ns_to_timespec ns_to_timespec64
38 # define timespec_add_ns timespec64_add_ns
41 static inline struct timespec
timespec64_to_timespec(const struct timespec64 ts64
)
45 ret
.tv_sec
= (time_t)ts64
.tv_sec
;
46 ret
.tv_nsec
= ts64
.tv_nsec
;
50 static inline struct timespec64
timespec_to_timespec64(const struct timespec ts
)
52 struct timespec64 ret
;
54 ret
.tv_sec
= ts
.tv_sec
;
55 ret
.tv_nsec
= ts
.tv_nsec
;
59 static inline int timespec_equal(const struct timespec
*a
,
60 const struct timespec
*b
)
62 return (a
->tv_sec
== b
->tv_sec
) && (a
->tv_nsec
== b
->tv_nsec
);
66 * lhs < rhs: return <0
67 * lhs == rhs: return 0
68 * lhs > rhs: return >0
70 static inline int timespec_compare(const struct timespec
*lhs
, const struct timespec
*rhs
)
72 if (lhs
->tv_sec
< rhs
->tv_sec
)
74 if (lhs
->tv_sec
> rhs
->tv_sec
)
76 return lhs
->tv_nsec
- rhs
->tv_nsec
;
79 extern void set_normalized_timespec(struct timespec
*ts
, time_t sec
, s64 nsec
);
81 static inline struct timespec
timespec_add(struct timespec lhs
,
84 struct timespec ts_delta
;
86 set_normalized_timespec(&ts_delta
, lhs
.tv_sec
+ rhs
.tv_sec
,
87 lhs
.tv_nsec
+ rhs
.tv_nsec
);
92 * sub = lhs - rhs, in normalized form
94 static inline struct timespec
timespec_sub(struct timespec lhs
,
97 struct timespec ts_delta
;
99 set_normalized_timespec(&ts_delta
, lhs
.tv_sec
- rhs
.tv_sec
,
100 lhs
.tv_nsec
- rhs
.tv_nsec
);
105 * Returns true if the timespec is norm, false if denorm:
107 static inline bool timespec_valid(const struct timespec
*ts
)
109 /* Dates before 1970 are bogus */
112 /* Can't have more nanoseconds then a second */
113 if ((unsigned long)ts
->tv_nsec
>= NSEC_PER_SEC
)
118 static inline bool timespec_valid_strict(const struct timespec
*ts
)
120 if (!timespec_valid(ts
))
122 /* Disallow values that could overflow ktime_t */
123 if ((unsigned long long)ts
->tv_sec
>= KTIME_SEC_MAX
)
129 * timespec_to_ns - Convert timespec to nanoseconds
130 * @ts: pointer to the timespec variable to be converted
132 * Returns the scalar nanosecond representation of the timespec
135 static inline s64
timespec_to_ns(const struct timespec
*ts
)
137 return ((s64
) ts
->tv_sec
* NSEC_PER_SEC
) + ts
->tv_nsec
;
141 * ns_to_timespec - Convert nanoseconds to timespec
142 * @nsec: the nanoseconds value to be converted
144 * Returns the timespec representation of the nsec parameter.
146 extern struct timespec
ns_to_timespec(const s64 nsec
);
149 * timespec_add_ns - Adds nanoseconds to a timespec
150 * @a: pointer to timespec to be incremented
151 * @ns: unsigned nanoseconds value to be added
153 * This must always be inlined because its used from the x86-64 vdso,
154 * which cannot call other kernel functions.
156 static __always_inline
void timespec_add_ns(struct timespec
*a
, u64 ns
)
158 a
->tv_sec
+= __iter_div_u64_rem(a
->tv_nsec
+ ns
, NSEC_PER_SEC
, &ns
);
165 * time_to_tm - converts the calendar time to local broken-down time
167 * @totalsecs the number of seconds elapsed since 00:00:00 on January 1, 1970,
168 * Coordinated Universal Time (UTC).
169 * @offset offset seconds adding to totalsecs.
170 * @result pointer to struct tm variable to receive broken-down time
172 static inline void time_to_tm(time_t totalsecs
, int offset
, struct tm
*result
)
174 time64_to_tm(totalsecs
, offset
, result
);
177 static inline unsigned long mktime(const unsigned int year
,
178 const unsigned int mon
, const unsigned int day
,
179 const unsigned int hour
, const unsigned int min
,
180 const unsigned int sec
)
182 return mktime64(year
, mon
, day
, hour
, min
, sec
);
185 static inline bool timeval_valid(const struct timeval
*tv
)
187 /* Dates before 1970 are bogus */
191 /* Can't have more microseconds then a second */
192 if (tv
->tv_usec
< 0 || tv
->tv_usec
>= USEC_PER_SEC
)
198 extern struct timespec
timespec_trunc(struct timespec t
, unsigned int gran
);
201 * timeval_to_ns - Convert timeval to nanoseconds
202 * @ts: pointer to the timeval variable to be converted
204 * Returns the scalar nanosecond representation of the timeval
207 static inline s64
timeval_to_ns(const struct timeval
*tv
)
209 return ((s64
) tv
->tv_sec
* NSEC_PER_SEC
) +
210 tv
->tv_usec
* NSEC_PER_USEC
;
214 * ns_to_timeval - Convert nanoseconds to timeval
215 * @nsec: the nanoseconds value to be converted
217 * Returns the timeval representation of the nsec parameter.
219 extern struct timeval
ns_to_timeval(const s64 nsec
);