0001
0002 #ifndef _LINUX_TIME64_H
0003 #define _LINUX_TIME64_H
0004
0005 #include <linux/math64.h>
0006 #include <vdso/time64.h>
0007
0008 typedef __s64 time64_t;
0009 typedef __u64 timeu64_t;
0010
0011 #include <uapi/linux/time.h>
0012
0013 struct timespec64 {
0014 time64_t tv_sec;
0015 long tv_nsec;
0016 };
0017
0018 struct itimerspec64 {
0019 struct timespec64 it_interval;
0020 struct timespec64 it_value;
0021 };
0022
0023
0024 #define PSEC_PER_NSEC 1000L
0025
0026
0027 #define TIME64_MAX ((s64)~((u64)1 << 63))
0028 #define TIME64_MIN (-TIME64_MAX - 1)
0029
0030 #define KTIME_MAX ((s64)~((u64)1 << 63))
0031 #define KTIME_MIN (-KTIME_MAX - 1)
0032 #define KTIME_SEC_MAX (KTIME_MAX / NSEC_PER_SEC)
0033 #define KTIME_SEC_MIN (KTIME_MIN / NSEC_PER_SEC)
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043 #define TIME_UPTIME_SEC_MAX (30LL * 365 * 24 *3600)
0044 #define TIME_SETTOD_SEC_MAX (KTIME_SEC_MAX - TIME_UPTIME_SEC_MAX)
0045
0046 static inline int timespec64_equal(const struct timespec64 *a,
0047 const struct timespec64 *b)
0048 {
0049 return (a->tv_sec == b->tv_sec) && (a->tv_nsec == b->tv_nsec);
0050 }
0051
0052
0053
0054
0055
0056
0057 static inline int timespec64_compare(const struct timespec64 *lhs, const struct timespec64 *rhs)
0058 {
0059 if (lhs->tv_sec < rhs->tv_sec)
0060 return -1;
0061 if (lhs->tv_sec > rhs->tv_sec)
0062 return 1;
0063 return lhs->tv_nsec - rhs->tv_nsec;
0064 }
0065
0066 extern void set_normalized_timespec64(struct timespec64 *ts, time64_t sec, s64 nsec);
0067
0068 static inline struct timespec64 timespec64_add(struct timespec64 lhs,
0069 struct timespec64 rhs)
0070 {
0071 struct timespec64 ts_delta;
0072 set_normalized_timespec64(&ts_delta, lhs.tv_sec + rhs.tv_sec,
0073 lhs.tv_nsec + rhs.tv_nsec);
0074 return ts_delta;
0075 }
0076
0077
0078
0079
0080 static inline struct timespec64 timespec64_sub(struct timespec64 lhs,
0081 struct timespec64 rhs)
0082 {
0083 struct timespec64 ts_delta;
0084 set_normalized_timespec64(&ts_delta, lhs.tv_sec - rhs.tv_sec,
0085 lhs.tv_nsec - rhs.tv_nsec);
0086 return ts_delta;
0087 }
0088
0089
0090
0091
0092 static inline bool timespec64_valid(const struct timespec64 *ts)
0093 {
0094
0095 if (ts->tv_sec < 0)
0096 return false;
0097
0098 if ((unsigned long)ts->tv_nsec >= NSEC_PER_SEC)
0099 return false;
0100 return true;
0101 }
0102
0103 static inline bool timespec64_valid_strict(const struct timespec64 *ts)
0104 {
0105 if (!timespec64_valid(ts))
0106 return false;
0107
0108 if ((unsigned long long)ts->tv_sec >= KTIME_SEC_MAX)
0109 return false;
0110 return true;
0111 }
0112
0113 static inline bool timespec64_valid_settod(const struct timespec64 *ts)
0114 {
0115 if (!timespec64_valid(ts))
0116 return false;
0117
0118 if ((unsigned long long)ts->tv_sec >= TIME_SETTOD_SEC_MAX)
0119 return false;
0120 return true;
0121 }
0122
0123
0124
0125
0126
0127
0128
0129
0130 static inline s64 timespec64_to_ns(const struct timespec64 *ts)
0131 {
0132
0133 if (ts->tv_sec >= KTIME_SEC_MAX)
0134 return KTIME_MAX;
0135
0136 if (ts->tv_sec <= KTIME_SEC_MIN)
0137 return KTIME_MIN;
0138
0139 return ((s64) ts->tv_sec * NSEC_PER_SEC) + ts->tv_nsec;
0140 }
0141
0142
0143
0144
0145
0146
0147
0148 extern struct timespec64 ns_to_timespec64(s64 nsec);
0149
0150
0151
0152
0153
0154
0155
0156
0157
0158 static __always_inline void timespec64_add_ns(struct timespec64 *a, u64 ns)
0159 {
0160 a->tv_sec += __iter_div_u64_rem(a->tv_nsec + ns, NSEC_PER_SEC, &ns);
0161 a->tv_nsec = ns;
0162 }
0163
0164
0165
0166
0167
0168 extern struct timespec64 timespec64_add_safe(const struct timespec64 lhs,
0169 const struct timespec64 rhs);
0170
0171 #endif